{"id":6729,"date":"2020-02-26T16:14:08","date_gmt":"2020-02-26T10:44:08","guid":{"rendered":"https:\/\/codeforgeek.com\/?p=6729"},"modified":"2023-12-22T19:05:32","modified_gmt":"2023-12-22T13:35:32","slug":"python-flask-tutorial-getting-started","status":"publish","type":"post","link":"https:\/\/codeforgeek.com\/python-flask-tutorial-getting-started\/","title":{"rendered":"Getting Started with Python Flask Framework"},"content":{"rendered":"<p>Welcome to this Flask tutorial series. In this series, we are going to learn about Flask &#8211; Python-based web framework. Flask is one of the most popular web frameworks for Python. Flask is easy to use, flexible and has less learning curve.<\/p>\n<p>You can checkout <a href=\"https:\/\/palletsprojects.com\/p\/flask\/\" rel=\"noopener noreferrer\" target=\"_blank\">Flask&#8217;s official <\/a>website to learn more about the Flask.<\/p>\n<p>In this post, we are going to install Flask in our system and code a simple &#8220;Hello World&#8221; application.<\/p>\n<p>Here is the Video version of the article.<\/p>\n<p><iframe width=\"560\" height=\"315\" src=\"https:\/\/www.youtube.com\/embed\/IltQhoFEa2Y\" frameborder=\"0\" allow=\"accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture\" allowfullscreen><\/iframe><\/p>\n<h2>Installing Flask<\/h2>\n<p>Before installing Flask, make sure your Python version is upgraded and it is version 2.7 or above. You can check the Python version using the following command.<\/p>\n<p><code><br \/>\n$ python --version<br \/>\n<\/code><\/p>\n<h3>Configuring Virtualenv<\/h3>\n<p>Virtualenv is a python environment builder. It helps you to create multiple Python environments in your system to avoid package and version conflict. This is a very handy and useful tool and I highly recommend you to use it.<\/p>\n<p>If you are running a Linux based system, you can install Virtualenv using the following command.<\/p>\n<p><code><br \/>\nsudo apt-get install virtualenv<br \/>\n<\/code><\/p>\n<p>You can also install it using pip.<\/p>\n<p><code><br \/>\npip install virtualenv<br \/>\n<\/code><\/p>\n<p>Now let&#8217;s install Flask. Create a new folder and switch to it using the terminal.<\/p>\n<p>Run this command to create a virtual environment.<\/p>\n<p><code><br \/>\nvirtualenv venv<br \/>\n<\/code><\/p>\n<p>Now activate the virtual environment using the following command.<\/p>\n<p><code><br \/>\nsource \/env\/bin\/activate<br \/>\n<\/code><\/p>\n<p>You should see <b>(env)<\/b> on the left side of the terminal.<\/p>\n<p>Install Flask using the following command.<\/p>\n<p><code><br \/>\npip install Flask<br \/>\n<\/code><\/p>\n<p>That&#8217;s it. You have successfully installed Flask in your machine.<\/p>\n<h2>Building First Flask Program<\/h2>\n<p>Before wasting any time, here is our code to return Hello World message when the API is hit.<\/p>\n<p><code lang='python'><br \/>\nfrom flask import Flask<br \/>\napp = Flask(__name__)<\/p>\n<p>@app.route('\/')<br \/>\ndef hello_world():<br \/>\n   return 'Hello World'<\/p>\n<p>if __name__ == '__main__':<br \/>\n   app.run()<br \/>\n<\/code><\/p>\n<p>Save the code in a file name <b>hello.py<\/b> and run it using the following command.<\/p>\n<p><code><br \/>\npython hello.py<br \/>\n<\/code><\/p>\n<p>Here is the output in the terminal.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/Screenshot-from-2020-02-26-15-56-17.png\" alt=\"\" width=\"950\" height=\"576\" class=\"alignnone size-full wp-image-6735\" srcset=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/Screenshot-from-2020-02-26-15-56-17.png 950w, https:\/\/codeforgeek.com\/wp-content\/uploads\/Screenshot-from-2020-02-26-15-56-17-300x182.png 300w, https:\/\/codeforgeek.com\/wp-content\/uploads\/Screenshot-from-2020-02-26-15-56-17-768x466.png 768w\" sizes=\"(max-width: 950px) 100vw, 950px\" \/><\/p>\n<p>Now open your browser and go to the <strong>localhost:5000<\/strong> URL.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/Screenshot-from-2020-02-26-15-57-37.png\" alt=\"\" width=\"987\" height=\"555\" class=\"alignnone size-full wp-image-6736\" srcset=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/Screenshot-from-2020-02-26-15-57-37.png 987w, https:\/\/codeforgeek.com\/wp-content\/uploads\/Screenshot-from-2020-02-26-15-57-37-300x169.png 300w, https:\/\/codeforgeek.com\/wp-content\/uploads\/Screenshot-from-2020-02-26-15-57-37-768x432.png 768w, https:\/\/codeforgeek.com\/wp-content\/uploads\/Screenshot-from-2020-02-26-15-57-37-400x225.png 400w\" sizes=\"(max-width: 987px) 100vw, 987px\" \/><\/p>\n<p>There you go, you just build the first program using the Python Flask framework. Let&#8217;s understand the code.<\/p>\n<p>First, we imported the Flask library in our code using the following line.<\/p>\n<p><code lang='python'><br \/>\nfrom flask import Flask<br \/>\n<\/code><\/p>\n<p>Then, we created a new instance of Flask and provided the name using the <b>__name__<\/b> variable.<\/p>\n<p><code lang='python'><br \/>\napp = Flask(__name__)<br \/>\n<\/code><\/p>\n<p>In the next line, we have created a new route. Routes are endpoints that can be used to design the structure of your web application. <\/p>\n<p><code lang='python'><br \/>\n@app.route('\/')<br \/>\ndef hello_world():<br \/>\n   return 'Hello World'<br \/>\n<\/code><\/p>\n<p>In the last line, we are are running our app. <\/p>\n<h2>Next<\/h2>\n<p>In the next post, we are going to learn about Flask routes and how to build API&#8217;s using Flask.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Welcome to this Flask tutorial series. In this series, we are going to learn about Flask &#8211; Python-based web framework. Flask is one of the most popular web frameworks for Python. Flask is easy to use, flexible and has less learning curve. You can checkout Flask&#8217;s official website to learn more about the Flask. In [&hellip;]<\/p>\n","protected":false},"author":69,"featured_media":6738,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_surecart_dashboard_logo_width":"180px","_surecart_dashboard_show_logo":true,"_surecart_dashboard_navigation_orders":true,"_surecart_dashboard_navigation_invoices":true,"_surecart_dashboard_navigation_subscriptions":true,"_surecart_dashboard_navigation_downloads":true,"_surecart_dashboard_navigation_billing":true,"_surecart_dashboard_navigation_account":true,"_uag_custom_page_level_css":"","footnotes":""},"categories":[191,134,18],"tags":[],"class_list":["post-6729","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-flask","category-python","category-tutorial"],"blocksy_meta":[],"uagb_featured_image_src":{"full":["https:\/\/codeforgeek.com\/wp-content\/uploads\/python-flask-tutorial.png",820,280,false],"thumbnail":["https:\/\/codeforgeek.com\/wp-content\/uploads\/python-flask-tutorial-150x150.png",150,150,true],"medium":["https:\/\/codeforgeek.com\/wp-content\/uploads\/python-flask-tutorial-300x102.png",300,102,true],"medium_large":["https:\/\/codeforgeek.com\/wp-content\/uploads\/python-flask-tutorial-768x262.png",768,262,true],"large":["https:\/\/codeforgeek.com\/wp-content\/uploads\/python-flask-tutorial.png",820,280,false],"1536x1536":["https:\/\/codeforgeek.com\/wp-content\/uploads\/python-flask-tutorial.png",820,280,false],"2048x2048":["https:\/\/codeforgeek.com\/wp-content\/uploads\/python-flask-tutorial.png",820,280,false]},"uagb_author_info":{"display_name":"Pankaj Kumar","author_link":"https:\/\/codeforgeek.com\/author\/pankaj\/"},"uagb_comment_info":0,"uagb_excerpt":"Welcome to this Flask tutorial series. In this series, we are going to learn about Flask &#8211; Python-based web framework. Flask is one of the most popular web frameworks for Python. Flask is easy to use, flexible and has less learning curve. You can checkout Flask&#8217;s official website to learn more about the Flask. In&hellip;","_links":{"self":[{"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/posts\/6729","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/users\/69"}],"replies":[{"embeddable":true,"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/comments?post=6729"}],"version-history":[{"count":0,"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/posts\/6729\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/media\/6738"}],"wp:attachment":[{"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/media?parent=6729"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/categories?post=6729"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/tags?post=6729"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}