{"id":297,"date":"2015-08-17T18:28:23","date_gmt":"2015-08-17T22:28:23","guid":{"rendered":"http:\/\/www.tayloraliss.com\/blog\/?p=297"},"modified":"2015-08-18T11:19:19","modified_gmt":"2015-08-18T15:19:19","slug":"finalizing-flask","status":"publish","type":"post","link":"https:\/\/www.tayloraliss.com\/blog\/?p=297","title":{"rendered":"Finalizing Flask"},"content":{"rendered":"<p>Ok, I just wanted to make sure I got this flask thing figured out. Here&#8217;s what I did and what happened when I did it.<\/p>\n<p>First I made a directory to house my test site. It&#8217;s as follows:<\/p>\n<p><code class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">c:\\code\\test\\mysite<\/code><\/p>\n<p>I then enter that directory to make sure any commands I do take place within it. I need a virtual environment to house Flask. When I install Flask, I don&#8217;t want to do it system-wide. This part was pretty simple too:<\/p>\n<p><code class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">virtualenv venv<\/code><\/p>\n<p>(Remember, the <code class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">venv<\/code> at the end is just the name of the directory the virtualenv will be installed into. You can actually name it anything you want, but \u00a0<code class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">venv<\/code> is standard practice.) After creating the virtual environment, I need to enter it. I do this by running the <code class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">activate<\/code> script in the <code class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">Scripts<\/code> sub-directory:<\/p>\n<p><code class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">venv\\Scripts\\activate<\/code><\/p>\n<p>The prompt now changes to show that the virtual environment is active. If I wish to exit the virtual environment, all I need to do is type <code class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">deactivate<\/code>. Finally, I need to install Flask into the virtual environment using <code class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">pip<\/code>. This is quite simple:<\/p>\n<p><code class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">pip install flask<\/code><\/p>\n<p>Boom! All done! Flask is installed into a virtual environment and ready to use. I should note that the file structure here is different from the one noted in\u00a0<a href=\"http:\/\/blog.miguelgrinberg.com\/post\/the-flask-mega-tutorial-part-ii-templates\">Miguel Grinberg&#8217;s Flask tutorial<\/a>. Miguels says that the file structure should look like this:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-linenumbers=\"false\">microblog\\\r\n\u00a0 \u00a0 flask\\\r\n        &lt;virtual environment files&gt;\r\n\u00a0 \u00a0 app\\\r\n\u00a0 \u00a0 \u00a0 \u00a0 static\\\r\n\u00a0 \u00a0 \u00a0 \u00a0 templates\\\r\n        __init__.py\r\n        views.py\r\n\u00a0 \u00a0 tmp\\\r\n    run.py<\/pre>\n<p>But I find that mine looks like this:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-linenumbers=\"false\">mysite\\\r\n\u00a0 \u00a0 venv\\\r\n\u00a0 \u00a0 \u00a0 \u00a0\u00a0include\\\r\n\u00a0 \u00a0 \u00a0 \u00a0\u00a0Lib\\\r\n\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0 __pycache__\\\r\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 collections\\\r\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 distutils\\\r\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 encodings\\\r\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 importlib\\\r\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 site-packages\\\r\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 flask\\\r\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 &lt;other packages&gt;\r\n\u00a0 \u00a0 \u00a0 \u00a0\u00a0Scripts\\<\/pre>\n<p>I can think of a few reasons why this might be. For starters,\u00a0this entry was last updated in September 2014. About a year ago. Flask may have changed a lot since then. Another reason for the difference might be that I&#8217;m doing this on Windows and Miguel was doing it on Linux and they change things between systems. I will have to investigate this more and find out why there are discrepancies.<\/p>\n<p>EDIT: Aha! I found one of the discrepancies! In the tutorial, Miguel uses the command <code class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">virtualenv flask<\/code> instead of <code class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">virtualenv venv<\/code>. That would explain why he has a folder titled &#8220;flask&#8221; in his <code class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">microblog<\/code> directory.<\/p>\n<p>Here&#8217;s the purpose of\u00a0the other folders he created:<\/p>\n<blockquote><p>The <span style=\"color: #ff0000;\"><code>app<\/code><\/span> folder will be where we will put our application package.<br \/>\nThe <span style=\"color: #ff0000;\"><code>static<\/code><\/span> sub-folder is where we will store static files like images, javascripts, and cascading style sheets.<br \/>\nThe <span style=\"color: #ff0000;\"><code>templates<\/code><\/span> sub-folder is obviously where our templates will go.<\/p><\/blockquote>\n<p>And here are the files that he created:<\/p>\n<p><code><span style=\"color: #ff0000;\">__init__.py<\/span><\/code><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\">from flask import Flask\r\n\r\napp = Flask(__name__)\r\nfrom app import views<\/pre>\n<p>The<code>\u00a0<span style=\"color: #ff0000;\">__init__.py<\/span><\/code>\u00a0file&#8217;s purpose is to create the application object (of class <span style=\"color: #ff0000;\"><code>Flask<\/code><\/span>) and then import the <span style=\"color: #ff0000;\"><code>views<\/code><\/span> module.\u00a0The views are the handlers that respond to requests from web browsers or other clients. In Flask handlers are written as Python functions. Each view function is mapped to one or more request URLs.<\/p>\n<p><code><span style=\"color: #ff0000;\">views.py<\/span><\/code><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\">from app import app\r\n\r\n@app.route('\/')\r\n@app.route('\/index')\r\ndef index():\r\n    return \"Hello, World!\"<\/pre>\n<p>The function in the <span style=\"color: #ff0000;\"><code>views.py<\/code> <\/span>file just returns a string, to be displayed on the client&#8217;s web browser. The two <span style=\"color: #ff0000;\"><code>route<\/code><\/span> decorators above the function create the mappings from URLs <span style=\"color: #ff0000;\"><code>\/<\/code><\/span> and\u00a0<span style=\"color: #ff0000;\"><code>\/index<\/code><\/span> to this function.<\/p>\n<p><span style=\"color: #ff0000;\"><code>run.py<\/code><\/span><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\">#!flask\/bin\/python\r\nfrom app import app\r\napp.run(debug=True)<\/pre>\n<p>The run.py script starts up the development web server with our application. It imports the <span style=\"color: #ff0000;\"><code>app<\/code><\/span> variable from our app package and invokes its <span style=\"color: #ff0000;\"><code>run<\/code><\/span> method to start the server.<\/p>\n<p>Finally, I should mention that I am a very visual learner, so the following drawing is my understanding of what&#8217;s happening when you enter\u00a0<code class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">flask\\Scripts\\python run.py<\/code>:<\/p>\n<p><a href=\"https:\/\/www.tayloraliss.com\/blog\/wp-content\/uploads\/2015\/08\/whats-going-on.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-medium wp-image-311\" src=\"https:\/\/www.tayloraliss.com\/blog\/wp-content\/uploads\/2015\/08\/whats-going-on-300x203.png\" alt=\"what's going on\" width=\"300\" height=\"203\" srcset=\"https:\/\/www.tayloraliss.com\/blog\/wp-content\/uploads\/2015\/08\/whats-going-on-300x203.png 300w, https:\/\/www.tayloraliss.com\/blog\/wp-content\/uploads\/2015\/08\/whats-going-on.png 786w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>Just a <a href=\"http:\/\/stackoverflow.com\/questions\/448271\/what-is-init-py-for\">final note<\/a>\u00a0on the <span style=\"color: #ff0000;\">_<code>_init__.py<\/code><\/span> file:\u00a0The <span style=\"color: #ff0000;\"><code>__init__.py<\/code><\/span> file makes Python treat directories containing it as modules.\u00a0Furthermore, it\u00a0is the first file to be loaded in a module, so you can use it to execute code that you want to run each time a module is loaded, or specify the submodules to be exported. It\u00a0allows you to define any variable at the package level<strong>.<\/strong> Doing so is often convenient if a package defines something that will be imported frequently.<\/p>\n<p>EDIT: One more thing! The first line of the <code><span style=\"color: #ff0000;\">run.py<\/span><\/code> file was enigmatic for me. I finally found out that it is a <a href=\"https:\/\/www.reddit.com\/r\/learnpython\/comments\/3hdmq5\/what_does_mean\/\">shebang line<\/a>. I&#8217;ll have to do more research on its purpose.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Ok, I just wanted to make sure I got this flask thing figured out. Here&#8217;s what I did and what happened when I did it. First I made a directory to house my test site. It&#8217;s as follows: c:\\code\\test\\mysite I then enter that directory to make sure any commands I do take place within it. [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_exactmetrics_skip_tracking":false,"_exactmetrics_sitenote_active":false,"_exactmetrics_sitenote_note":"","_exactmetrics_sitenote_category":0,"footnotes":""},"categories":[1],"tags":[],"class_list":["post-297","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/www.tayloraliss.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/297","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.tayloraliss.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tayloraliss.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tayloraliss.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tayloraliss.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=297"}],"version-history":[{"count":10,"href":"https:\/\/www.tayloraliss.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/297\/revisions"}],"predecessor-version":[{"id":316,"href":"https:\/\/www.tayloraliss.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/297\/revisions\/316"}],"wp:attachment":[{"href":"https:\/\/www.tayloraliss.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=297"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tayloraliss.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=297"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tayloraliss.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=297"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}