{"id":5778,"date":"2018-01-07T17:49:46","date_gmt":"2018-01-07T17:49:46","guid":{"rendered":"http:\/\/putridparrot.com\/blog\/?p=5778"},"modified":"2018-01-07T17:49:46","modified_gmt":"2018-01-07T17:49:46","slug":"python-rest-service-using-web-py","status":"publish","type":"post","link":"https:\/\/putridparrot.com\/blog\/python-rest-service-using-web-py\/","title":{"rendered":"Python REST service using web.py"},"content":{"rendered":"<p>First off, run either of the following commands from PyCharm&#8217;s Terminal window (or the command line, ofcourse).<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\npip install web.py\r\n# or\r\neasy_install web.py\r\n<\/pre>\n<p>From PyCharm you can also go to File | Settings, then locate Project Interpreter, for your project. Here you can press the + button, search for web.py then press the Install Package button.<\/p>\n<p>If we take the <a href=\"http:\/\/webpy.org\/\" rel=\"noopener\" target=\"_blank\">Getting Started<\/a> code from the web.py website and place into a .py file<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nimport web\r\n\r\nurls = (\r\n    '\/(.*)', 'hello'\r\n)\r\napp = web.application(urls, globals())\r\n\r\n\r\nclass hello:\r\n    def GET(self, name):\r\n        if not name:\r\n            name = 'World'\r\n        return 'Hello, ' + name + '!'\r\n\r\n\r\nif __name__ == &quot;__main__&quot;:\r\n    app.run()\r\n<\/pre>\n<p>Run this and you should see the output http:\/\/0.0.0.0:8080. Now, from your preferred web browser type http:\/\/localhost:8080. The default result will be &#8220;Hello hello!&#8221; if you now type http:\/\/localhost:8080\/PutridParrot you&#8217;ll see &#8220;Hello PutridParrot!&#8221;.<\/p>\n<p>So what&#8217;s happening&#8230;?<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nurls = (\r\n    '\/(.*)', 'hello'\r\n)\r\napp = web.application(urls, globals())\r\n<\/pre>\n<p>The web.application creates a web applications with the supplied url&#8217;s and the classes that each url maps to. In this instance all url&#8217;s map to the <em>hello<\/em> class. Now the hello class is <\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nclass hello:\r\n    def GET(self, name):\r\n        if not name:\r\n            name = 'World'\r\n        return 'Hello, ' + name + '!'\r\n<\/pre>\n<p>So this class takes all url&#8217;s and defines a function for each HTTP method accepts, the case maps to that sent via the browser, hence is uppercase GET (if you make a mistake here you&#8217;ll see a Method Not Allowed error from your REST service).<\/p>\n<p>Our GET method takes an argument, in the case of http:\/\/localhost:8080\/PutridParrot this argument is Putridparrot. If no argument is supplied the code sets <em>name<\/em> to &#8216;World&#8217;. You get the idea.<\/p>\n<p>Finally we need to run the service application, hence the code<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nif __name__ == &quot;__main__&quot;:\r\n    app.run()\r\n<\/pre>\n<p><strong>Templating<\/strong><\/p>\n<p>So this is all well and good, but the output is plain text, what if we are serving web pages, it&#8217;d be a lot better to be able to write HTML and embed our Python code in it, like PHP, ASP etc. <\/p>\n<p>web.py includes a templating capability, so if we create a folder for our templates (let&#8217;s call it templates) off of our service source. Then add an HTML file, let&#8217;s call it index.html, now remove the HTML from this and replace with <\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\n$def with(name)\r\n\r\n$if name:\r\n   Hello &lt;em&gt;$name&lt;\/em&gt;\r\n$else:\r\n   Hello &lt;em&gt;World&lt;\/em&gt;\r\n<\/pre>\n<p>We&#8217;ve got the first line as <em>$def with(name)<\/em> which basically states that our template is called with the variable <em>name<\/em> which we then use within the rest of the template. The $ obviously preceeds our Python statements, but other than that it&#8217;s probably pretty obvious what&#8217;s going on here and fairly standard looking for Python.<\/p>\n<p>Now change our python source to look like this<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nimport web\r\n\r\nurls = (\r\n    '\/(.*)', 'index'\r\n)\r\n\r\nrender = web.template.render('templates\/')\r\n\r\nclass index:\r\n    def GET(self, name):\r\n        return render.index(name)\r\n\r\n\r\nif __name__ == &quot;__main__&quot;:\r\n    app = web.application(urls, globals())\r\n    app.run()\r\n<\/pre>\n<p>We need to tell web.py where our templates reside, hence the line<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nrender = web.template.render('templates\/')\r\n<\/pre>\n<p>Now I&#8217;ve changed the class etc. to be our index class, this wasn&#8217;t necessary as <em>render.index<\/em> maps to our HTML file name, but I figured I&#8217;d try and make things more like a real world application.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>First off, run either of the following commands from PyCharm&#8217;s Terminal window (or the command line, ofcourse). pip install web.py # or easy_install web.py From PyCharm you can also go to File | Settings, then locate Project Interpreter, for your project. Here you can press the + button, search for web.py then press the Install [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[195],"tags":[],"class_list":["post-5778","post","type-post","status-publish","format-standard","hentry","category-python"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/5778","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/comments?post=5778"}],"version-history":[{"count":4,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/5778\/revisions"}],"predecessor-version":[{"id":5802,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/5778\/revisions\/5802"}],"wp:attachment":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/media?parent=5778"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/categories?post=5778"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/tags?post=5778"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}