{"id":839,"date":"2024-02-23T11:33:58","date_gmt":"2024-02-23T11:33:58","guid":{"rendered":"https:\/\/learnpython.elegantwallp.com\/?p=839"},"modified":"2024-02-23T11:34:01","modified_gmt":"2024-02-23T11:34:01","slug":"first-program","status":"publish","type":"post","link":"https:\/\/learnpython.elegantwallp.com\/2024\/02\/23\/first-program\/","title":{"rendered":"First Program"},"content":{"rendered":"\n<p>TurboGears has a minimal mode that makes it possible to create single file applications quickly. Simple examples and services can be built quickly with minimal set of dependencies.<\/p>\n\n\n\n<p>Application class in a TG application is inherited from&nbsp;<strong>TGController<\/strong>&nbsp;class. Methods in this class are available for access by&nbsp;<strong>@expose<\/strong>&nbsp;decorator from&nbsp;<strong>tg<\/strong>&nbsp;module. In our first application,&nbsp;<strong>index()<\/strong>&nbsp;method is mapped as root of our application. The TGController class also needs to be imported from&nbsp;<strong>tg<\/strong>&nbsp;module.from tg import expose, TGController class MyController(TGController): @expose() def index(self): return &#8216;Hello World turbogears&#8217;<\/p>\n\n\n\n<p>Next, set the application\u2019s configuration and declare application object.\u00a0<strong>AppConfig<\/strong>\u00a0class constructor here takes two parameters \u2013 minimal attribute set to true and the controller class.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>config = AppConfig(minimal = True, root_controller = RootController()) application = config.make_wsgi_app()<\/code><\/pre>\n\n\n\n<p>The&nbsp;<strong>make_wsgi_app()<\/strong>&nbsp;function here constructs application object.<\/p>\n\n\n\n<p>In order to serve this application, we now need to start the HTTP server. As mentioned earlier, we shall use\u00a0<strong>simple_server<\/strong>\u00a0module in\u00a0<strong>wsgiref<\/strong>\u00a0package to set up and start it. This module has\u00a0<strong>make_server()<\/strong>\u00a0method which requires port number and application object as arguments.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>from wsgiref.simple_server import make_server server = make_server('', 8080, application) server.serve_forever()<\/code><\/pre>\n\n\n\n<p>It means that our application is going to be served at port number 8080 of localhost.<\/p>\n\n\n\n<p>The following is the complete code of our first TurboGears application \u2212<\/p>\n\n\n\n<p><strong>app.py<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>from wsgiref.simple_server import make_server from tg import expose, TGController, AppConfig class MyController(TGController): @expose() def index(self): return 'Hello World TurboGears' config = AppConfig(minimal = True, root_controller = MyController()) application = config.make_wsgi_app() print \"Serving on port 8080...\" server = make_server('', 8080, application) server.serve_forever()<\/code><\/pre>\n\n\n\n<p>Run the above script from Python shell.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Python app.py<\/code><\/pre>\n\n\n\n<p>Enter&nbsp;<strong>http:\/\/localhost:8080<\/strong>&nbsp;in browser\u2019s address bar to view \u2018Hello World TurboGears\u2019 message.<\/p>\n\n\n\n<p>The\u00a0<strong>tg.devtools<\/strong>\u00a0of TurboGears contains Gearbox. It is a set of commands, which are useful for management of more complex TG projects. Full stack projects can be quickly created by the following Gearbox command \u2212<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>gearbox quickstart HelloWorld<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>TurboGears has a minimal mode that makes it possible to create single file applications quickly. Simple examples and services can be built quickly with minimal set of dependencies. Application class in a TG application is inherited from&nbsp;TGController&nbsp;class. Methods in this class are available for access by&nbsp;@expose&nbsp;decorator from&nbsp;tg&nbsp;module. In our first application,&nbsp;index()&nbsp;method is mapped as root [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[88],"tags":[],"class_list":["post-839","post","type-post","status-publish","format-standard","hentry","category-turbo-gears"],"_links":{"self":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts\/839","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/comments?post=839"}],"version-history":[{"count":1,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts\/839\/revisions"}],"predecessor-version":[{"id":840,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts\/839\/revisions\/840"}],"wp:attachment":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/media?parent=839"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/categories?post=839"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/tags?post=839"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}