-
Install Python Mac OS comes with python 2 installed but this tutorial uses python 3. Install it with homebrew
-
$ brew install python3
-
Create a directory for your app
$ mkdir micro_services
-
Install virtualenv
$ cd micro_services
$ pip install virtualenv
$ pip install virtualenvwrapper
- Edit your
.bash_profile
$ vi ~/.bash_profile
- Add the following
export WORKON_HOME=$HOME/.virtualenvs
export PROJECT_HOME=$HOME/Devel
export VIRTUALENVWRAPPER_PYTHON=/usr/local/bin/python
export VIRTUALENVWRAPPER_VIRTUALENV=/usr/local/bin/virtualenv
source /usr/local/bin/virtualenvwrapper.sh
-
Hit the
esc
key on your keyboard to exit VIM and:wq
keys to save your changes. If you made an error exit withesc
key and:q!
to exit without saving changes. -
Change directories, then create and activate a new virtual environment
$ mkvirtualenv micro_services
$ workon micro_services
- Check the python version
python --version
Python 3.7.6
- Install Flask
(micro_services) $ pip install flask
- Create a new package called app
(micro_services) $ mkdir app
(micro_services) $ touch app/__init__.py
In Python, a sub-directory that includes a init.py file is considered a package. When you import a package, the init.py executes and defines what symbols the package exposes to the internet.
- Add the following to the init.py for the app package
# app/__init__.py: Flask application instance
from flask import Flask
app = Flask(__name__)
from app import routes
The script above simply creates the application object as an instance of class Flask imported from the flask package. The name variable passed to the Flask class is a Python predefined variable, which is set to the name of the module in which it is used. Flask uses the location of the module passed here as a starting point when it needs to load associated resources such as template files. Notice the bottom import , it is a workaround to circular imports, a common problem with Flask applications. The routes don’t exist yet, so let’s create it next.
-
Create a routes module
(micro_services) $ touch app/routes.py
-
Add the following view function to the new routes.py file.
# app/routes.py
from app import app
@app.route('/')
@app.route('/index')
def index():
return "Hello, World!"
The view function simply returns a “Hello World” string. The decorator @app.route
creates the association between the given url and the function. If the browser requests either of the associated urls /
or /index
Flask is going to invoke the index function and pass the return value to the browser as a response.
- To complete this simple application, create a top-level Python script to define the Flask application instance.
(micro_services) $ touch micro_services.py
- Define the application instance and import it
# micro_services.py
from app import app
Our simple application is now complete , but before running it we need to tell Flask how to import it by setting a FLASK_APP
environment variable.
(micro_services) $ export FLASK_APP=micro_services.py
- Finally let’s run our first web application with the following command
(micro_services) $ flask run
* Serving Flask app "micro_services"
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
- View your application by pointing your browser to the IP address indicated by Flask, in this case
http://127.0.0.1:5000/
Your application should look similar to the following
Congratulations on building your first Flask app. Before finalizing this article let’s do some housekeeping to keep our app clean and maintainable. It also reinforces best practices.
- Stop the server by pressing
Ctrl-C
- Save your dependencies to a
requirements.txt
file(micro_services) $ pip freeze > requirements.txt
This automatically creates the file for you and it is best practice to allow other to collaborate on your project . Flask installs a few other dependencies so don’t worry if you see them in your file.
# requirements.txt
Click==7.0
Flask==1.1.1
itsdangerous==1.1.0
Jinja2==2.11.1
MarkupSafe==1.1.1
Werkzeug==1.0.0
- Also remember to deactivate you virtual environment to prevent installing other decencies not related to this project
(micro_services) $ deactivate
That’s it! you built your first Flask app. Give me some claps if liked this article or found it helpful. Thanks for reading.
#blog/medium/flask/getting-stated