{"id":375,"date":"2017-09-06T14:53:28","date_gmt":"2017-09-06T19:53:28","guid":{"rendered":"http:\/\/bluegalaxy.info\/codewalk\/?p=375"},"modified":"2019-06-15T16:46:50","modified_gmt":"2019-06-15T21:46:50","slug":"flask-how-to-get-up-and-running-in-less-than-5-minutes","status":"publish","type":"post","link":"https:\/\/bluegalaxy.info\/codewalk\/2017\/09\/06\/flask-how-to-get-up-and-running-in-less-than-5-minutes\/","title":{"rendered":"Flask: How to get up and running in less than 5 minutes"},"content":{"rendered":"<p>The following instructions are for running Flask on Windows with Python 3 (Anaconda).<\/p>\n<p>To run Flask, you will first need to install Flask in your Python Scripts folder. To do this, run `pip install flask` in a command window. Then confirm that the installation was successful by loading python and giving it the <code>import flask<\/code>\u00a0command.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-378\" src=\"http:\/\/bluegalaxy.info\/codewalk\/wp-content\/uploads\/2017\/09\/import_flask-300x46.png\" alt=\"\" width=\"709\" height=\"109\" srcset=\"https:\/\/bluegalaxy.info\/codewalk\/wp-content\/uploads\/2017\/09\/import_flask-300x46.png 300w, https:\/\/bluegalaxy.info\/codewalk\/wp-content\/uploads\/2017\/09\/import_flask-768x118.png 768w, https:\/\/bluegalaxy.info\/codewalk\/wp-content\/uploads\/2017\/09\/import_flask-676x104.png 676w, https:\/\/bluegalaxy.info\/codewalk\/wp-content\/uploads\/2017\/09\/import_flask.png 863w\" sizes=\"auto, (max-width: 709px) 100vw, 709px\" \/><\/p>\n<p>If there is no error when importing Flask, it means that Flask is installed.<\/p>\n<p>Next, create a Flask folder that you will run your Flask app from. For this example I created a &#8216;flask_test&#8217; folder on my desktop.<\/p>\n<p>Then you will need to create a .py script written for Flask and place it in the folder. Note: After you run this script, Flask will automatically create a __pycache__ folder. For example:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-382\" src=\"http:\/\/bluegalaxy.info\/codewalk\/wp-content\/uploads\/2017\/09\/flask_folder.png\" alt=\"\" width=\"220\" height=\"145\" \/><\/p>\n<p>Let&#8217;s back up a step and actually create the code in the hello.py script. The following code represents a very basic Flask script:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\">from flask import Flask\n\napp = Flask(__name__)\n\n@app.route('\/')\n@app.route('\/hello')\ndef hello_world():\n    return 'Hello World!'\n\n@app.route('\/esp')\ndef hola_mundo():\n    return 'Hola mundo!'<\/pre>\n<p>Now that the code is saved as hello.py and placed in the flask_test folder, we can run it via the following command prompt commands one at a time. Note: for command window, I am using <a href=\"https:\/\/www.fosshub.com\/ConEmu.html\">ConEmu (x64)<\/a>, but the cmd.exe that comes with Windows would work also.<\/p>\n<ol>\n<li style=\"list-style-type: none;\">\n<ol>\n<li>cd to the location of your Python installation: <code class=\"EnlighterJSRAW\" data-enlighter-language=\"raw\">cd C:\\Users\\christon\\AppData\\Local\\Continuum\\Anaconda3<\/code><\/li>\n<li>Tell Flask what python script you want to run: <code class=\"EnlighterJSRAW\" data-enlighter-language=\"raw\">set FLASK_APP=C:\\Users\\christon\\Desktop\\flask_test\\hello.py<\/code><\/li>\n<li>Turn on Flask debug mode: <code class=\"EnlighterJSRAW\" data-enlighter-language=\"raw\">set FLASK_DEBUG=1<\/code><\/li>\n<li>Tell python to run flask: <code class=\"EnlighterJSRAW\" data-enlighter-language=\"raw\">python -m flask run<\/code>Note: &#8216;-m&#8217; means module name.<\/li>\n<\/ol>\n<\/li>\n<\/ol>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-387\" src=\"http:\/\/bluegalaxy.info\/codewalk\/wp-content\/uploads\/2017\/09\/flask_commands-300x149.png\" alt=\"\" width=\"584\" height=\"290\" srcset=\"https:\/\/bluegalaxy.info\/codewalk\/wp-content\/uploads\/2017\/09\/flask_commands-300x149.png 300w, https:\/\/bluegalaxy.info\/codewalk\/wp-content\/uploads\/2017\/09\/flask_commands.png 591w\" sizes=\"auto, (max-width: 584px) 100vw, 584px\" \/><\/p>\n<p>Finally, point your browser at the server location generated by Flask. This can be either http:\/\/127.0.0.1:5000 or http:\/\/localhost:5000. For example, if you point your browser at default url \/ or \/hello:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-390\" src=\"http:\/\/bluegalaxy.info\/codewalk\/wp-content\/uploads\/2017\/09\/hello_world.png\" alt=\"\" width=\"169\" height=\"81\" \/><\/p>\n<p>If you point your browser at \/esp, Flask will route you accordingly:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-391\" src=\"http:\/\/bluegalaxy.info\/codewalk\/wp-content\/uploads\/2017\/09\/hola_mundo.png\" alt=\"\" width=\"193\" height=\"82\" \/><\/p>\n<p>To recap, here are the steps to get up and running with Flask:<\/p>\n<ol>\n<li>Make sure Flask is installed in your Python Scripts folder using pip install<\/li>\n<li>Confirm that flask is installed in Python using <code class=\"EnlighterJSRAW\" data-enlighter-language=\"python\">import flask<\/code><\/li>\n<li>Create a Flask project folder<\/li>\n<li>Create your .py script and drop it into the flask folder<\/li>\n<li>Run commands to initiate Flask via command prompt<\/li>\n<li>Point your browser at the web server that Flask provides<\/li>\n<\/ol>\n<p>Notes: When you make changes to your .py script, you should see the changes in your web browser when reloading.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The following instructions are for running Flask on Windows with Python 3 (Anaconda). To run Flask, you will first need to install Flask in your Python Scripts folder. To do this, run `pip install flask` in a command window. Then confirm that the installation was successful by loading python and giving it the import flask\u00a0command. &hellip; <a href=\"https:\/\/bluegalaxy.info\/codewalk\/2017\/09\/06\/flask-how-to-get-up-and-running-in-less-than-5-minutes\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Flask: How to get up and running in less than 5 minutes<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[28],"tags":[29,4],"class_list":["post-375","post","type-post","status-publish","format-standard","hentry","category-flask","tag-flask","tag-python"],"_links":{"self":[{"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/posts\/375","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/comments?post=375"}],"version-history":[{"count":22,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/posts\/375\/revisions"}],"predecessor-version":[{"id":2786,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/posts\/375\/revisions\/2786"}],"wp:attachment":[{"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/media?parent=375"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/categories?post=375"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/tags?post=375"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}