What is TurboGears in Python?

Posted in /  

What is TurboGears in Python?

Vinay Khatri
Last updated on June 9, 2022

    A Python web framework is a collection of different modules or libraries used to create web-application using Python programming. A web framework makes it easy for the developer to write the logic for dynamic web applications without worrying about the complexities of web development like web protocols and web interfaces. This tutorial will have an Introduction to TurboGears framework in Python. Here is the list of some popular Python web frameworks

    • Django
    • Flask
    • Pyramid
    • Bottle
    • CheeryPy
    • TurboGears

    And in this tutorial, we will have an Introduction to the Python TurboGears framework.

    What is TurboGears?

    TurboGears2 is a next-generation Python web framework that follows the MVC  Architecture like popular frameworks Struts and Ruby on Rails. It is a full-stack web framework, but with its minimal interface and single page script, it can act as a micro web framework when we do not want the full stack use of TurboGears. It comes with many dependencies such as WSGI for server web-page interaction, SQLAlchemy for database, and Genshi & Repoze for Markup templating.

    Components

    As a FullStack web framework, It is built on many libraries and middleware. There are two main series of TurboGears. TurboGears 1.x and TurboGears 2.x, between which 2.x is the latest series. The latest version is TurboGears 2.4.3, also referred to as TurboGears2. The TurboGears2 series introduced "minimal mode", which allows users to treat it as a microframework when full-stack features of TurboGears are not required. Here is the list of all the main components that TurboGears2 offers.

    1. SQLAlchemy: SQLAlchemy is an Object Relational Mapper(ORM) toolkit that helps users create and interact with SQL databases using Python objects .
    2. Ming: It uses Ming to deal with NoSQL databases like MongoDB.
    3. Genshi: Genshi is a Python template engine, and TurboGears uses it to insert data in HTML or XHTML pages.
    4. Repoze: To handle security like authorization and custom rules, TurboGears uses Repoze.
    5. ToscaWidgets: To create complex forms and GUIs, TurboGears depend on its built-in component called ToscaWidgets. Tosca can generate complex and straightforward HTML forms and connect JavaScript widgets and toolkits.
    6. Gearbox: Although the web app can be connected to Apache, Nginx, or any other WSGI server, it provides a built-in Gearbox toolkit to manage its Project.

    Features

    1. It can act as a micro as well as a full-stack web framework.
    2. Comes with a powerful Object Relational Mapper (ORM) and supports multi-database.
    3. It follows MVC Architecture.
    4. Comes with a built-in templating engine like Genshi.
    5. Provides FomeEncode for validation.
    6. It also includes a toolkit like Repoze for web security like authorization.
    7. It also provides a command-line tool for project management.
    8. Comes with built-in ToscaWidgets to display backend data on the front-end design.
    9. Support Front-Faching WSGI-based server.
    10. Use functions and decorators to write the view logic for the web application.

    Get Started with Python TurboGears Framework

    It is a third-party, open-source Python web framework; before we write our first TurboGears web app, we need to install it using the pip install command for our Python environment.

    Install Python TurboGears framework

    We need to type the simple pip install command on our command prompt or terminal, and it will install it for our Python environment.

    pip install TurboGears2

    You can also activate a Python virtual environment and then install TurboGears for your Project. Now let's write our First Python TurboGears Hello World Web App. As the Hello World Program is straightforward, we can make it using TurboGears minimal mode as a single TurboGears Python app script. app.py

    from wsgiref.simple_server import make_server
    from tg import MinimalApplicationConfigurator
    from tg import expose, TGController
    
    # RootController of our web app, in charge of serving content for /
    class RootController(TGController):
     @expose()
     def index(self):
         return 'Hello World! Welcome to TurboGears'
    
    # configure the minimal applicaiton 
    config = MinimalApplicationConfigurator()
    config.update_blueprint({
     'root_controller': RootController()
    })
    
    # run the server
    print("Server running on port 8080 (Open localhost:8080 on your browser)")
    httpd = make_server('', 8080, config.make_wsgi_app())
    httpd.serve_forever()
    

    Now execute the program on the terminal or command prompt using the python command.

    python app.py

    After executing the app.py, it will run a localhost server on port 8080. To see the web app running, open http://localhost:8080/ on your web browser.

    Conclusion

    Now let's conclude this article; here, we learned how to write a simple Hello World Program. The core development team is now primarily focusing on the TurboGears2.x series because it provides more features than TurboGears1.x, and it is also inclined toward the modern Framework. With built-in ORM and Template engine tools, It is becoming a perfect alternative to the pyramid framework , a "starts small and can finish big" Framework. People are also reading:

    Leave a Comment on this Post

    0 Comments