{"id":565,"date":"2019-05-17T06:13:22","date_gmt":"2019-05-17T06:13:22","guid":{"rendered":"https:\/\/www.triptera.com.au\/wordpress\/?p=565"},"modified":"2019-05-17T06:17:27","modified_gmt":"2019-05-17T06:17:27","slug":"flask-hello-world-in-pycharm-edu","status":"publish","type":"post","link":"https:\/\/www.triptera.com.au\/wordpress\/2019\/05\/17\/flask-hello-world-in-pycharm-edu\/","title":{"rendered":"Flask &#8220;Hello World!&#8221; in PyCharm Edu"},"content":{"rendered":"\n<p>On their website, Flask provide a simple <a href=\"http:\/\/flask.pocoo.org\">&#8220;Hello World!&#8221; demo<\/a> that you can run from the command line. I have written this post to show how the same thing can be achieved using PyCharm Edu. It is more steps, but once it is set up, the web application can be run by a menu item or keystroke in PyCharm Edu without ever having to use the terminal. Jetbrains recommend PyCharm Professional for Flask development work but with this technique all you need is one of the free IDEs PyCharm Edu or PyCharm Community. <\/p>\n\n\n\n<h2 class=\"wp-block-heading\">TL;DR (too long; didn&#8217;t read)<\/h2>\n\n\n\n<p>If you are already familiar with PyCharm Edu, the steps are essentially:<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>Create a new python project with a virtual environment, and &#8220;Flask&#8221; in your requirements.txt file.<\/li><li>Create a new Python file hello.py as per the <a href=\"http:\/\/flask.pocoo.org\">Flask demo<\/a>.<\/li><li>If you are using PyCharm Edu you will need to enable the &#8220;Run&#8221; menu.<\/li><li>Create and run a new &#8220;Python&#8221; run configuration:<ol><li>Name: flask run hello<\/li><li>Module (not Script): flask<\/li><li>Parameters: run<\/li><li>Environment variables: PYTHONUBUFFERED=1;FLASK_APP=hello.py;FLASK_ENV=development<\/li><\/ol><\/li><\/ol>\n\n\n\n<h2 class=\"wp-block-heading\">Detailed instructions with pictures<\/h2>\n\n\n\n<p>These screen shots have been taken from PyCharm Edu 2019.1 using Python 3.7.3 on Windows 10 Pro.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"310\" height=\"331\" src=\"https:\/\/www.triptera.com.au\/wordpress\/wp-content\/uploads\/2019\/05\/flask-hello-step-01-create-new-project.png\" alt=\"\" class=\"wp-image-566\" srcset=\"https:\/\/www.triptera.com.au\/wordpress\/wp-content\/uploads\/2019\/05\/flask-hello-step-01-create-new-project.png 310w, https:\/\/www.triptera.com.au\/wordpress\/wp-content\/uploads\/2019\/05\/flask-hello-step-01-create-new-project-281x300.png 281w\" sizes=\"auto, (max-width: 310px) 100vw, 310px\" \/><figcaption>Create a new project in PyCharm Edu<\/figcaption><\/figure>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"663\" height=\"105\" src=\"https:\/\/www.triptera.com.au\/wordpress\/wp-content\/uploads\/2019\/05\/flask-hello-step-02-name-project.png\" alt=\"\" class=\"wp-image-567\" srcset=\"https:\/\/www.triptera.com.au\/wordpress\/wp-content\/uploads\/2019\/05\/flask-hello-step-02-name-project.png 663w, https:\/\/www.triptera.com.au\/wordpress\/wp-content\/uploads\/2019\/05\/flask-hello-step-02-name-project-300x48.png 300w\" sizes=\"auto, (max-width: 663px) 100vw, 663px\" \/><figcaption>Name your project &#8220;hello-flask&#8221;. This will also create a new virtual environment.<\/figcaption><\/figure>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"414\" height=\"189\" src=\"https:\/\/www.triptera.com.au\/wordpress\/wp-content\/uploads\/2019\/05\/flask-hello-step-04-open-project-folder.png\" alt=\"\" class=\"wp-image-569\" srcset=\"https:\/\/www.triptera.com.au\/wordpress\/wp-content\/uploads\/2019\/05\/flask-hello-step-04-open-project-folder.png 414w, https:\/\/www.triptera.com.au\/wordpress\/wp-content\/uploads\/2019\/05\/flask-hello-step-04-open-project-folder-300x137.png 300w\" sizes=\"auto, (max-width: 414px) 100vw, 414px\" \/><figcaption>Click on &#8220;hello-flask&#8221; to select the project folder. Then click on > next to &#8220;hello-flask&#8221; to expand the folder and show the contents. Depending on where you are saving your virtual environments you may or may not see &#8220;venv&#8221;.<\/figcaption><\/figure>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"401\" height=\"121\" src=\"https:\/\/www.triptera.com.au\/wordpress\/wp-content\/uploads\/2019\/05\/flask-hello-step-05-file-new.png\" alt=\"\" class=\"wp-image-570\" srcset=\"https:\/\/www.triptera.com.au\/wordpress\/wp-content\/uploads\/2019\/05\/flask-hello-step-05-file-new.png 401w, https:\/\/www.triptera.com.au\/wordpress\/wp-content\/uploads\/2019\/05\/flask-hello-step-05-file-new-300x91.png 300w\" sizes=\"auto, (max-width: 401px) 100vw, 401px\" \/><figcaption>Now that the project folder is selected, you can select &#8220;New&#8230;&#8221; from the &#8220;File&#8221; menu to create a new file in the project folder.<\/figcaption><\/figure>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"396\" height=\"244\" src=\"https:\/\/www.triptera.com.au\/wordpress\/wp-content\/uploads\/2019\/05\/flask-hello-step-06-file-new-file.png\" alt=\"\" class=\"wp-image-571\" srcset=\"https:\/\/www.triptera.com.au\/wordpress\/wp-content\/uploads\/2019\/05\/flask-hello-step-06-file-new-file.png 396w, https:\/\/www.triptera.com.au\/wordpress\/wp-content\/uploads\/2019\/05\/flask-hello-step-06-file-new-file-300x185.png 300w\" sizes=\"auto, (max-width: 396px) 100vw, 396px\" \/><figcaption>A floating menu will appear asking you the type of file. We are creating a text file so choose &#8220;File&#8221; which can be used for any file type.<\/figcaption><\/figure>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"325\" height=\"123\" src=\"https:\/\/www.triptera.com.au\/wordpress\/wp-content\/uploads\/2019\/05\/flask-hello-step-07-name-requirements.txt.png\" alt=\"\" class=\"wp-image-573\" srcset=\"https:\/\/www.triptera.com.au\/wordpress\/wp-content\/uploads\/2019\/05\/flask-hello-step-07-name-requirements.txt.png 325w, https:\/\/www.triptera.com.au\/wordpress\/wp-content\/uploads\/2019\/05\/flask-hello-step-07-name-requirements.txt-300x114.png 300w\" sizes=\"auto, (max-width: 325px) 100vw, 325px\" \/><figcaption>Name the file requirements.txt and click &#8220;OK&#8221;.<\/figcaption><\/figure>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"914\" height=\"200\" src=\"https:\/\/www.triptera.com.au\/wordpress\/wp-content\/uploads\/2019\/05\/flask-hello-step-07-requirements.txt.png\" alt=\"\" class=\"wp-image-572\" srcset=\"https:\/\/www.triptera.com.au\/wordpress\/wp-content\/uploads\/2019\/05\/flask-hello-step-07-requirements.txt.png 914w, https:\/\/www.triptera.com.au\/wordpress\/wp-content\/uploads\/2019\/05\/flask-hello-step-07-requirements.txt-300x66.png 300w, https:\/\/www.triptera.com.au\/wordpress\/wp-content\/uploads\/2019\/05\/flask-hello-step-07-requirements.txt-768x168.png 768w\" sizes=\"auto, (max-width: 914px) 100vw, 914px\" \/><figcaption>In the requirements.txt type the word &#8220;Flask&#8221;. This replaces the step &#8220;pip install Flask&#8221; from the command line demo. After you have typed in &#8220;Flask&#8221; click on the recommendation to &#8220;Install requirement&#8221;. The recommendation may not come straight away so continue on to the next steps and it should appear before you get to running the web application.<\/figcaption><\/figure>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"778\" height=\"234\" src=\"https:\/\/www.triptera.com.au\/wordpress\/wp-content\/uploads\/2019\/05\/flask-hello-step-08-new-python-file.py_.png\" alt=\"\" class=\"wp-image-576\" srcset=\"https:\/\/www.triptera.com.au\/wordpress\/wp-content\/uploads\/2019\/05\/flask-hello-step-08-new-python-file.py_.png 778w, https:\/\/www.triptera.com.au\/wordpress\/wp-content\/uploads\/2019\/05\/flask-hello-step-08-new-python-file.py_-300x90.png 300w, https:\/\/www.triptera.com.au\/wordpress\/wp-content\/uploads\/2019\/05\/flask-hello-step-08-new-python-file.py_-768x231.png 768w\" sizes=\"auto, (max-width: 778px) 100vw, 778px\" \/><figcaption>To create a new Python file, right click on the project folder and select New > Python File.<\/figcaption><\/figure>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"342\" height=\"130\" src=\"https:\/\/www.triptera.com.au\/wordpress\/wp-content\/uploads\/2019\/05\/flask-hello-step-08-hello.py_.png\" alt=\"\" class=\"wp-image-575\" srcset=\"https:\/\/www.triptera.com.au\/wordpress\/wp-content\/uploads\/2019\/05\/flask-hello-step-08-hello.py_.png 342w, https:\/\/www.triptera.com.au\/wordpress\/wp-content\/uploads\/2019\/05\/flask-hello-step-08-hello.py_-300x114.png 300w\" sizes=\"auto, (max-width: 342px) 100vw, 342px\" \/><figcaption>Because PyCharm knows it is going to be a Python file, we don&#8217;t need to type in .py on the end. Just type in the name &#8220;hello&#8221; and click &#8220;OK&#8221;.<\/figcaption><\/figure>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"285\" height=\"147\" src=\"https:\/\/www.triptera.com.au\/wordpress\/wp-content\/uploads\/2019\/05\/flask-hello-step-10-hello.py-code.png\" alt=\"\" class=\"wp-image-577\"\/><figcaption>In hello.py, type in the code from the <a href=\"http:\/\/flask.pocoo.org\">Flask &#8220;Hello World!&#8221; demo<\/a>. The file will get saved automatically.<\/figcaption><\/figure>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"289\" height=\"285\" src=\"https:\/\/www.triptera.com.au\/wordpress\/wp-content\/uploads\/2019\/05\/flask-hello-step-11-settings.png\" alt=\"\" class=\"wp-image-578\"\/><figcaption>If you are using PyCharm Community Edition you can skip the next three steps which are only required to enable some menus which are hidden in PyCharm Edu but visible in PyCharm Community Edition. Click on &#8220;Settings&#8230;&#8221; in the &#8220;File&#8221; menu. On Mac this menu is called &#8220;Preferences&#8221;.<\/figcaption><\/figure>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"574\" height=\"591\" src=\"https:\/\/www.triptera.com.au\/wordpress\/wp-content\/uploads\/2019\/05\/flask-hello-step-12-menu-add-action.png\" alt=\"\" class=\"wp-image-579\" srcset=\"https:\/\/www.triptera.com.au\/wordpress\/wp-content\/uploads\/2019\/05\/flask-hello-step-12-menu-add-action.png 574w, https:\/\/www.triptera.com.au\/wordpress\/wp-content\/uploads\/2019\/05\/flask-hello-step-12-menu-add-action-291x300.png 291w\" sizes=\"auto, (max-width: 574px) 100vw, 574px\" \/><figcaption>The main menu is missing some menus so:<br \/>1. Expand &#8220;Appearance and Behaviour&#8221;<br \/>2. Select &#8220;Menus and Toolbars&#8221;<br \/>3. Expand &#8220;Main menu&#8221;<br \/>4. Select &#8220;Code&#8221; menu so we can add new menus after it<br \/>5. Click on &#8220;+&#8221; to add extra menus <br \/>6. Click &#8220;Add Action&#8230;&#8221;<\/figcaption><\/figure>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"326\" height=\"544\" src=\"https:\/\/www.triptera.com.au\/wordpress\/wp-content\/uploads\/2019\/05\/flask-hello-step-13-refactor-to-window.png\" alt=\"\" class=\"wp-image-580\" srcset=\"https:\/\/www.triptera.com.au\/wordpress\/wp-content\/uploads\/2019\/05\/flask-hello-step-13-refactor-to-window.png 326w, https:\/\/www.triptera.com.au\/wordpress\/wp-content\/uploads\/2019\/05\/flask-hello-step-13-refactor-to-window-180x300.png 180w\" sizes=\"auto, (max-width: 326px) 100vw, 326px\" \/><figcaption>Select all menus between &#8220;Refactor&#8221; and &#8220;Window&#8221; and click on &#8220;OK&#8221;.<\/figcaption><\/figure>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"736\" height=\"421\" src=\"https:\/\/www.triptera.com.au\/wordpress\/wp-content\/uploads\/2019\/05\/flask-hello-step-14-menu-ok.png\" alt=\"\" class=\"wp-image-581\" srcset=\"https:\/\/www.triptera.com.au\/wordpress\/wp-content\/uploads\/2019\/05\/flask-hello-step-14-menu-ok.png 736w, https:\/\/www.triptera.com.au\/wordpress\/wp-content\/uploads\/2019\/05\/flask-hello-step-14-menu-ok-300x172.png 300w\" sizes=\"auto, (max-width: 736px) 100vw, 736px\" \/><figcaption>The menus are now included so click &#8220;OK&#8221;<\/figcaption><\/figure>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"622\" height=\"198\" src=\"https:\/\/www.triptera.com.au\/wordpress\/wp-content\/uploads\/2019\/05\/flask-hello-step-15-run-edit-configurations.png\" alt=\"\" class=\"wp-image-582\" srcset=\"https:\/\/www.triptera.com.au\/wordpress\/wp-content\/uploads\/2019\/05\/flask-hello-step-15-run-edit-configurations.png 622w, https:\/\/www.triptera.com.au\/wordpress\/wp-content\/uploads\/2019\/05\/flask-hello-step-15-run-edit-configurations-300x95.png 300w\" sizes=\"auto, (max-width: 622px) 100vw, 622px\" \/><figcaption>To run a flask app we need to create a &#8220;run configuration&#8221;. Select &#8220;Edit configurations&#8230;&#8221; from the &#8220;Run&#8221; menu.<\/figcaption><\/figure>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"247\" height=\"219\" src=\"https:\/\/www.triptera.com.au\/wordpress\/wp-content\/uploads\/2019\/05\/flask-hello-step-16-add-python-configuration.png\" alt=\"\" class=\"wp-image-583\"\/><figcaption>Click &#8220;+&#8221; to add a new configuration and select &#8220;Python&#8221;.<\/figcaption><\/figure>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"645\" src=\"https:\/\/www.triptera.com.au\/wordpress\/wp-content\/uploads\/2019\/05\/flask-hello-step-17-run-edit-configurations-save-1024x645.png\" alt=\"\" class=\"wp-image-584\" srcset=\"https:\/\/www.triptera.com.au\/wordpress\/wp-content\/uploads\/2019\/05\/flask-hello-step-17-run-edit-configurations-save-1024x645.png 1024w, https:\/\/www.triptera.com.au\/wordpress\/wp-content\/uploads\/2019\/05\/flask-hello-step-17-run-edit-configurations-save-300x189.png 300w, https:\/\/www.triptera.com.au\/wordpress\/wp-content\/uploads\/2019\/05\/flask-hello-step-17-run-edit-configurations-save-768x484.png 768w, https:\/\/www.triptera.com.au\/wordpress\/wp-content\/uploads\/2019\/05\/flask-hello-step-17-run-edit-configurations-save.png 1076w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><figcaption>1. Name your configuration &#8220;flask run hello&#8221; or something else if you prefer.<br \/>2. Change label from &#8220;Script path:&#8221; to &#8220;Module name:&#8221; <br \/>3. Type &#8220;flask&#8221; as the module name<br \/>4. Type &#8220;run&#8221; as the parameter<br \/>5. Configure environment variables by clicking box. (See next screenshot)<br \/>6. After setting environment variables (next screenshot), click &#8220;OK&#8221;<\/figcaption><\/figure>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"516\" height=\"393\" src=\"https:\/\/www.triptera.com.au\/wordpress\/wp-content\/uploads\/2019\/05\/flask-hello-step-18-environment.png\" alt=\"\" class=\"wp-image-585\" srcset=\"https:\/\/www.triptera.com.au\/wordpress\/wp-content\/uploads\/2019\/05\/flask-hello-step-18-environment.png 516w, https:\/\/www.triptera.com.au\/wordpress\/wp-content\/uploads\/2019\/05\/flask-hello-step-18-environment-300x228.png 300w\" sizes=\"auto, (max-width: 516px) 100vw, 516px\" \/><figcaption>1. &#8220;+&#8221; to add a new environment variable<br \/>2. Name it &#8220;FLASK_APP&#8221;<br \/>3. Give it value &#8220;hello.py&#8221;<br \/>4. &#8220;+&#8221; to add another environment variable<br \/>5. Name it &#8220;FLASK_ENV&#8221;<br \/>6. Give it value &#8220;development&#8221;<br \/>7. Click &#8220;OK&#8221; to return to previous dialog box and click &#8220;OK&#8221; again.<\/figcaption><\/figure>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"379\" height=\"187\" src=\"https:\/\/www.triptera.com.au\/wordpress\/wp-content\/uploads\/2019\/05\/flask-hello-step-19-run-flask-run-hello.png\" alt=\"\" class=\"wp-image-586\" srcset=\"https:\/\/www.triptera.com.au\/wordpress\/wp-content\/uploads\/2019\/05\/flask-hello-step-19-run-flask-run-hello.png 379w, https:\/\/www.triptera.com.au\/wordpress\/wp-content\/uploads\/2019\/05\/flask-hello-step-19-run-flask-run-hello-300x148.png 300w\" sizes=\"auto, (max-width: 379px) 100vw, 379px\" \/><figcaption>Now you can run the builtin web application server by selecting &#8220;Run &#8216;flask run hello'&#8221; from the Run menu. If the first item does not say &#8216;flask run hello&#8217; then choose &#8220;Run&#8230;&#8221; (third item in menu) and select &#8216;flask run hello&#8217; from the popup menu.<\/figcaption><\/figure>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"518\" height=\"242\" src=\"https:\/\/www.triptera.com.au\/wordpress\/wp-content\/uploads\/2019\/05\/flask-hello-step-20-run-status.png\" alt=\"\" class=\"wp-image-587\" srcset=\"https:\/\/www.triptera.com.au\/wordpress\/wp-content\/uploads\/2019\/05\/flask-hello-step-20-run-status.png 518w, https:\/\/www.triptera.com.au\/wordpress\/wp-content\/uploads\/2019\/05\/flask-hello-step-20-run-status-300x140.png 300w\" sizes=\"auto, (max-width: 518px) 100vw, 518px\" \/><figcaption>A run status pane will open at the bottom of the screen. You can click on the URL to see the outpu of your web application in a web browser. Click the red square to stop your application.<\/figcaption><\/figure>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"434\" height=\"164\" src=\"https:\/\/www.triptera.com.au\/wordpress\/wp-content\/uploads\/2019\/05\/flask-hello-step-21-web.png\" alt=\"\" class=\"wp-image-588\" srcset=\"https:\/\/www.triptera.com.au\/wordpress\/wp-content\/uploads\/2019\/05\/flask-hello-step-21-web.png 434w, https:\/\/www.triptera.com.au\/wordpress\/wp-content\/uploads\/2019\/05\/flask-hello-step-21-web-300x113.png 300w\" sizes=\"auto, (max-width: 434px) 100vw, 434px\" \/><figcaption>This is what your web browser should show you. Yay!!! \ud83d\ude42<\/figcaption><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>On their website, Flask provide a simple &#8220;Hello World!&#8221; demo that you can run from the command line. I have written this post to show how the same thing can be achieved using PyCharm Edu. It is more steps, but once it is set up, the web application can be run by a menu item [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_coblocks_attr":"","_coblocks_dimensions":"","_coblocks_responsive_height":"","_coblocks_accordion_ie_support":"","footnotes":""},"categories":[1],"tags":[],"class_list":["post-565","post","type-post","status-publish","format-standard","hentry","category-playing"],"_links":{"self":[{"href":"https:\/\/www.triptera.com.au\/wordpress\/wp-json\/wp\/v2\/posts\/565","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.triptera.com.au\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.triptera.com.au\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.triptera.com.au\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.triptera.com.au\/wordpress\/wp-json\/wp\/v2\/comments?post=565"}],"version-history":[{"count":3,"href":"https:\/\/www.triptera.com.au\/wordpress\/wp-json\/wp\/v2\/posts\/565\/revisions"}],"predecessor-version":[{"id":590,"href":"https:\/\/www.triptera.com.au\/wordpress\/wp-json\/wp\/v2\/posts\/565\/revisions\/590"}],"wp:attachment":[{"href":"https:\/\/www.triptera.com.au\/wordpress\/wp-json\/wp\/v2\/media?parent=565"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.triptera.com.au\/wordpress\/wp-json\/wp\/v2\/categories?post=565"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.triptera.com.au\/wordpress\/wp-json\/wp\/v2\/tags?post=565"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}