TurboGears2 Documentation

repository·development·Indexed 21 days ago

https://github.com/turbogears/tg2

A hybrid Python web framework that operates as either a lightweight microframework for single-file applications or a comprehensive full-stack framework.

Tokens
526
Snippets
2
Records
3
Agent score
24%

What's inside TurboGears2

  1. How TurboGears works as a Full Stack vs Microframework

    development

    TurboGears is a hybrid web framework.

    • Microframework mode: Can be used in a single file for minimal applications (as shown in the quickstart guide).
    • Full Stack mode: Provides more extensive capabilities. For full-stack examples and tools, refer to tg.devtools.
  2. Create a minimal TurboGears application

    development

    TurboGears can be used as a microframework in single-file mode. To create a minimal application, define a class inheriting from TGController, use the @expose decorator to make methods accessible via URLs, and use MinimalApplicationConfigurator to configure the application and map your root controller.

    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(content_type="text/plain")
        def index(self):
            return 'Hello World'
    
    # Configure a new minimal application with our root controller.
    config = MinimalApplicationConfigurator()
    config.update_blueprint({
        'root_controller': RootController()
    })
    
    # Serve the newly configured web application.
    print("Serving on port 8080...")
    httpd = make_server('', 8080, config.make_wsgi_app())
    httpd.serve_forever()