{"id":5781,"date":"2019-11-03T21:44:52","date_gmt":"2019-11-03T21:44:52","guid":{"rendered":"http:\/\/putridparrot.com\/blog\/?p=5781"},"modified":"2019-11-03T21:44:52","modified_gmt":"2019-11-03T21:44:52","slug":"python-rest-service-using-flask","status":"publish","type":"post","link":"https:\/\/putridparrot.com\/blog\/python-rest-service-using-flask\/","title":{"rendered":"Python REST service using Flask"},"content":{"rendered":"<p>Wanting to create a Python based REST service? Let&#8217;s use Flask and see what we can do.<\/p>\n<p>Start off by installing Flask<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\npip install Flask\r\n<\/pre>\n<p>Now let&#8217;s write some code<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nfrom flask import Flask\r\napp = Flask(__name__)\r\n\r\n@app.route(&quot;\/&quot;)\r\ndef hello():\r\n    return &quot;Hello World!&quot;\r\n\r\napp.run()\r\n<\/pre>\n<p>This is nice and simple and pretty obvious how things work. As you can see we create the Flash application and in this sample run it (although on the Flask website they often show running the Flask Run from the Python command line).<\/p>\n<p>Each &#8220;route&#8221; is defined and mapped directly to a function. In this example the root of the URL is mapped to the hello function. <\/p>\n<p>The route can also contain variables which are show within &lt;&gt;, for example<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\n@app.route('\/&lt;name&gt;')\r\ndef hello(name):\r\n    return f&quot;Hello {name}&quot;\r\n<\/pre>\n<p>We can also define converters (similar to declaring the type of the variable) for the variables, so for example if we need to access something by an integer id, the we might have<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\n@app.route('\/employee\/&lt;int:id&gt;')\r\ndef employee(id):\r\n    return f&quot;Employee {id}&quot;\r\n<\/pre>\n<p>By default the converter used (as you&#8217;d probably expect) is <em>string<\/em>, but Flask also supports <em>int<\/em>, <em>float<\/em>, <em>path<\/em>, <em>any<\/em> and <em>uuid<\/em>.<\/p>\n<p>By default each route is using the HTTP method GET, but we can also define the supported methods for each route, i.e.<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\n@app.route('\/login', methods=&#x5B;'GET', 'POST'])\r\ndef login():\r\n    if request.method == 'POST':\r\n        do_the_login()\r\n    else:\r\n        show_the_login_form()\r\n<\/pre>\n<p><strong>References<\/strong><\/p>\n<p><a href=\"http:\/\/flask.pocoo.org\/\" rel=\"noopener noreferrer\" target=\"_blank\">Flask is Fun<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Wanting to create a Python based REST service? Let&#8217;s use Flask and see what we can do. Start off by installing Flask pip install Flask Now let&#8217;s write some code from flask import Flask app = Flask(__name__) @app.route(&quot;\/&quot;) def hello(): return &quot;Hello World!&quot; app.run() This is nice and simple and pretty obvious how things work. [&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-5781","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\/5781","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=5781"}],"version-history":[{"count":3,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/5781\/revisions"}],"predecessor-version":[{"id":7604,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/5781\/revisions\/7604"}],"wp:attachment":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/media?parent=5781"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/categories?post=5781"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/tags?post=5781"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}