Getting Started with Flask
Flask is a lightweight python web framework and it depends on the jinja template engine and Werkzeug WZGI tool kit.
It was originally designed and developed by an Austrian open source software programmer Armin Ronacher as an April fools day joke in 2010.
It is considered to be more pythonic than Django web framework because it is lighter and more explicit. Trust me if you’re new to web development but not to python, Flask will be very easy for you because you will feel as though you’re working with vanilla python.
Now let’s get started. In the next few minutes, you will have created your first web application in flask
Create a directory where your flask project will be hosted using the command below
mkdir my_flask_app
Inside your Directory, create another directory
mkdir flaskr
cd flaskr
Inside the flaskr directory create an init file
touch __init__.py
Create a virtual environment. The purpose of the virtual environment is to create an isolated environment for python projects. You can read more about virtual environments here.
If you do not have a virtual environment installed, in your terminal just run the command below
pip install virtualenv
Now that you have a virtual environment on your local machine, use the command below to set it up in your project
virtualenv venv
Activate the virtual environment using the command below
source venv/Scripts/activate --- if you're using windows
or
source venv/bin/activate --- if you're using linux
With your virtual environment activated Install flask using the command below
pip install flask
Now inside your init file in the flaskr directory, write these lines of code
from flask import Flaskdef create_app(test_config=None):
app = (__name__) @app.route('/')
def index():
return "Hello there I can run" return app
Now you’re almost 100% done 💪
Next in your terminal run the following commands
export FLASK_APP=flaskr
export FLASK_ENV=development
flask run
By now your server should be running and I’m pretty sure it’s running on http://127.0.0.1:5000/.
Congrats you’ve built your first web app using flask