DEV Community

Cover image for WebForms.py Update to WebFormsJS 1.6
Elanat Framework
Elanat Framework

Posted on

WebForms.py Update to WebFormsJS 1.6

The WebForms Core technology continues to evolve, and with the release of WebFormsJS 1.6, the Python implementation of the WebForms class has been updated to fully support the latest features. This update ensures seamless communication between the server-side WebForms class and the client-side WebFormsJS library, enabling developers to create highly dynamic and interactive web applications with ease.

WebForms Core is a new technology developed by Elanat. It was initially added as a new feature to Elanat's CodeBehind framework, but now Elanat has made WebForms Core available in all programming languages.

The video below shows the functionality of WebForms Core technology.

Download Links

You can download the WebForms class for the Python programming language from the link below.

WebForms.py

To download the latest version of WebFormsJS, visit the following link.

Download WebFormsJS

Example: Using WebForms Core in Flask

Below is an example of how to use the updated WebForms class in a Flask application. This example demonstrates how to dynamically change the text color of textarea elements based on user interactions like drag enter, select, and key up events.

View

from flask import Flask, request
from WebForms import WebForms, HtmlEvent

app = Flask(__name__)

@app.route('/')
def index():

    form = WebForms()

    if 'set-color' in request.args:
        color = request.args.get('set-color')
        index = request.args.get('index')

        form.set_text_color(f'<textarea>{index}', color)
        form.set_text_color(f'<textarea>{index}', 'unset')
        form.assign_delay(3)

        return form.response()

    html_content = """
    <!DOCTYPE html>
    <html>
    <head>
      <title>Using WebForms Core</title>
      <script type="text/javascript" src="/script/web-forms.js"></script>
      <style>
        textarea
        {
            width: 600px;
            height: 200px;
            margin-bottom: 20px;
        }
      </style>
    </head>
    <body>
        <h1>WebForms Core Technology in Python</h1>
        <b>WebForms Core technology (drag enter event)</b>
        <br>
        <textarea>WebForms Core is a revolutionary, high-level technology for manipulating DOM elements from the server. This technology was introduced by Elanat in 2024 and is available across all programming languages ​​and back-end frameworks. WebForms Core manages HTML tags on the server side, allowing developers to focus on server responses without worrying about the front end. This technology uses a JavaScript library called WebFormsJS to facilitate communication between the server and client. WebForms Core offers a robust infrastructure for web development, ensuring efficient processing and minimal server load. It represents a significant advancement in server-side web development, making it easier for developers to create dynamic and responsive web applications.</textarea>
        <br>
        <b>CodeBehind Framework (select event)</b>
        <br>
        <textarea>CodeBehind Framework is a modern full-stack framework developed by Elanat in 2023. Built under ASP.NET Core, it competes with Microsoft's default web frameworks like ASP.NET Core MVC, Razor Pages, and Blazor. CodeBehind offers a nostalgic yet innovative approach reminiscent of Microsoft's former WebForms, providing a modular and flexible structure. It supports various development patterns, including MVC, Model-View, Controller-View, and only View, ensuring high server-side independence and simplicity. This framework is known for its speed, simplicity, and adaptability, making it a powerful alternative to ASP.NET Core.</textarea>
        <br>
        <b>Elanat CMS (key up event)</b>
        <br>
        <textarea>Elanat CMS is a content management system also developed by Elanat, leveraging the CodeBehind Framework. It is designed to be modular and powerful, allowing developers to create and manage various add-ons seamlessly. Elanat CMS supports eight types of add-ons, including components, modules, plugins, pages, patches, and fetch. The CMS is built on a modern architecture that facilitates dynamic add-ons and Ajax postbacks, ensuring a smooth and efficient web development process. With its advanced Ajax capabilities and modular structure, Elanat CMS stands out as a robust solution for managing web content.</textarea>
    """

    if 'set-color' not in request.args:
        form.set_get_event('<textarea>0', HtmlEvent.on_drag_enter, '?set-color=sienna&index=0')
        form.set_get_event('<textarea>1', HtmlEvent.on_select, '?set-color=blue&index=1')
        form.set_get_event('<textarea>2', HtmlEvent.on_key_up, '?set-color=green&index=2')

        html_content += form.export_to_web_forms_tag()

    html_content += """
    </body>
    </html>
    """

    return html_content

if __name__ == '__main__':
    app.run(debug=True)
Enter fullscreen mode Exit fullscreen mode

The GIF image below shows how the above code works.
WebForms Core Technology in Python

in the upper code of script file, the script starts with including the "WebForms.py" file, which contains the WebForms class.

Check events

It creates a new instance of the WebForms class.

Check

...
form = WebForms()

if 'set-color' in request.args:
    color = request.args.get('set-color')
    index = request.args.get('index')

    form.set_text_color(f'<textarea>{index}', color)
    form.set_text_color(f'<textarea>{index}', 'unset')
    form.assign_delay(3)

    return form.response()
...
Enter fullscreen mode Exit fullscreen mode

In the provided code, an instance of the WebForms class is first created.

If the query parameters contain "set-color", it retrieves the color and index from the request.

It sets the text color of a specific "textarea" element identified by its index to the specified color and then resets it to "unset" after a delay of 3 seconds.

Initialization for set events

Set event

...
if 'set-color' not in request.args:
    form.set_get_event('<textarea>0', HtmlEvent.on_drag_enter, '?set-color=sienna&index=0')
    form.set_get_event('<textarea>1', HtmlEvent.on_select, '?set-color=blue&index=1')
    form.set_get_event('<textarea>2', HtmlEvent.on_key_up, '?set-color=green&index=2')

    html_content += form.export_to_web_forms_tag()
...
Enter fullscreen mode Exit fullscreen mode

Finally, it returns the response generated by the "form".

If "set-color" is not present in the request arguments, it sets up event handlers for each textarea:

  • "on_drag_enter" : Changes color to sienna when dragging enters textarea 0.
  • "on_select": Changes color to blue when textarea 1 is selected.
  • "on_key_up": Changes color to green when a key is released in textarea 2.

It also appends any additional HTML generated by:
form.export_to_web_forms_tag()

Note: Please note that when the HTML page is requested for the first time, if you are using the WebForms class, use the export_to_web_forms_tag method. For secondary requests, use the response method.

You can get the sample code in link below.
https://elanat.net/content/28/WebForms.py Updated to WebFormsJS 1.6.html

Conclusion

This update to WebForms.py ensures that Python developers can take full advantage of the latest features in WebFormsJS 1.6, making it easier than ever to build dynamic, server-driven web applications. Whether you're using Flask, Django, or another Python framework, WebForms Core provides a powerful and flexible solution for modern web development.

Top comments (0)