Beginners Introduction to Flask

8 minutes
1 year ago
<h2><b>Overview</b></h2><p>1. An introduction to Flask</p><p>2. Hello, World! web-app</p><h2><b>Introduction</b></h2><p>This article is an introduction to Flask, a microframework for Python based on Werkzeug, Jinja 2 and good intentions.</p><p><b>What are frameworks?</b></p><p>Frameworks are a special case of software libraries in that they are reusable abstractions of code wrapped in a well-defined API.</p><p>Simply, a framework is a collection of classes built using the programming language they are based on, so that you don’t have to reinvent the wheel every time you start a new project.</p><p>Django is another very popular framework based on Python.</p><p><b>Why is this important?</b></p><p>If you have just starting your journey as a Python developer, learning Flask will really help you expand your abilities as a developer, be it making a simple REST API, or to make a full-featured web-app (something like Reddit for example) in a reasonable amount of time, and the concepts of Web Development you will learn along the way will definitely help in some way in a variety of projects that you will work on throughout your career.</p><p>Some common use cases of Flask include building an API and have it up and running in no time. In addition to that, it comes bundled with templating libraries Werkzeug and Jinja2 that will allow you to build a full fledged blog with it.</p><h2><b>Content</b></h2><h2><b>Installation</b></h2><h3><b>Installing Python</b></h3><p>If you already haven’t, install Python from https://www.python.org/</p><p>To check if Python is correctly installed and configured, open a terminal and enter the command ‘python’. If installed correctly, you should have the python interpreter running</p><p><img src="https://i.imgur.com/oo93G1N.png"><br></p><p>Next step,</p><h3><b>Installing pip</b></h3><p>Pip is a package management system used to install and manage software packages written in Python.</p><p>To install it, </p><p>1. https://bootstrap.pypa.io/get-pip.py </p><p>Copy the code from the above link, save it as get_pip.py </p><p>2. Run the script </p><pre>$ python get_pip.py</pre><p>To install a package with pip, use the command - pip install &lt;package-name&gt;</p><h3><b>Installing Flask</b></h3><p>To install Flask, we will now run the command </p><pre>$ pip install flask</pre><p>To check if Flask is installed correctly, open the Python interpreter and import the Flask package&nbsp;</p><p><img src="https://i.imgur.com/FhzFJwX.png"><br></p><h3><b>Hello World web-app in Flask</b></h3><p>As is customary, we’ll now write a Hello World program with Flask.</p><pre># 1. import the flask package<br>from flask import Flask<br># 2. create an app of type Flask<br>app = Flask(__name__)<br># 3. define a route (or address) which will handle the request<br>@app.route('/')<br>def index():<br> return ("Hello World!")<br># 4. run the app<br>if __name__ == "__main__":<br> app.run(debug=True)</pre><p>Save the file as app.py, and from the terminal run it as</p><pre>$ python app.py</pre><p>You should get something like this.&nbsp;<br></p><p><img src="https://i.imgur.com/xiYm3P1.png"><br></p><p>Open you web browser and go to http://127.0.0.1:5000/ or http://localhost:5000/<br></p><p><img src="https://i.imgur.com/HT79DEE.png"><br></p><h2><b>Conclusion</b></h2><p>And there you have it! A simple Hello World program in Flask.</p><p>To know more about what the classes and functions mentioned do, you can check out the documentation.</p><p>Tip : You can use the ‘help()’ class to know more about any given class in your program.</p><p> </p><p>In the next tutorial, we will learn ‘How to make a REST API with Flask’ and deploy it on Heroku.</p><p>Till then, keep learning!</p>

Comments

You must login to comment