This tutorial is meant for people coming from other languages than Python or from other frameworks than Flask
At first glance
When you come from another language or framework, learning Flask at first may be a time-consuming task.
- You will have to familiarize with the peculiarities of the langue like list comprehension and tuple unpacking. These consist of a lot of syntactic sugar that helps you write less code.
- You will need to understand the import system underlying in Python.
- You will have to find out a way to structure your app so that you won’t end up repeating code uselessly and the imports will not cause circular dependency issues.
- You will need to familiarize with SQL Alchemy.
For the purpose of this tutorial, I will use the code from my FOSS project Adhesive
The final structure of our app will look like this:
Lets’ start creating our environment.
I assume you have at least one version of Python interpreter installed on your machine.
Next step is to install pip on your system. This part varies according to your operative system.
At this point, you will have to set up a local environment in order not to affect the global dependencies of your Python installation.
After trying multiple ways to create a local environment, my recommendation is to use the old venv.
venv -p /path/to/python /path/to/your/project
Once this is done you can create your setup.py file that will be needed later on in order to distribute your app.
You can change this example according to your own needs.
import os from setuptools import find_packages, setup with open(os.path.join(os.path.dirname(__file__), 'README.rst')) as readme: README = readme.read() # allow setup.py to be run from any path os.chdir(os.path.normpath(os.path.join(os.path.abspath(__file__), os.pardir))) setup( name='adhesive', version='0.1', packages=find_packages(), include_package_data=True, license='MIT License', # example license description='Custom made middleman app', long_description=README, url='https://www.enligthening.io/', author='Davide Pugliese', email@example.com', classifiers=[ 'Environment :: Web Environment', 'Framework :: Flask', 'Framework :: Flask :: 0.12.2', 'Intended Audience :: Developers', 'License :: MIT License', # example license 'Operating System :: OS Independent', 'Programming Language :: Python', 'Programming Language :: Python :: 3', 'Programming Language :: Python :: 3.6', 'Topic :: Internet :: WWW/HTTP', 'Topic :: Internet :: WWW/HTTP :: Dynamic Content', ], )
setuptools is a library that contains a function, find_packages, which finds all the packages that are needed by your app. This way you will not need to maintain manually the list of the packages being used.
We can proceed to install flask by running
pip install flask
Now, let’s create the run.py file which is the initiator of our app.
from theroot import app app.run(debug=True, host='0.0.0.0', port=5001)
At this point, we can move forward and create our config.py file which contains all of the configuration settings that are used globally by our app.
As an example you can consider this code
import datetime class Config(object): DEBUG = False TESTING = False SQLALCHEMY_DATABASE_URI = 'mysql+pymysql://firstname.lastname@example.org/yourdb' THREADS_PER_PAGE = 4 CSRF_ENABLED = True CSRF_SESSION_KEY = "secret" JWT_SECRET_KEY = 'your-secret' JWT_ACCESS_TOKEN_EXPIRES = datetime.timedelta(minutes=60) GOOGLE_MAP_API_KEY = your_map_api_key GOOGLE_PLACES_API_KEY = your_places_api_key class ProductionConfig(Config): SQLALCHEMY_DATABASE_URI = 'mysql+pymysql://email@example.com/yourdb' class DevelopmentConfig(Config): DEBUG = True SQLALCHEMY_DATABASE_URI = 'mysql+pymysql://firstname.lastname@example.org/yourdb' class TestingConfig(Config): TESTING = True
So, right now we have installed our local environment, we have installed Flask and we have our configuration object in place.
In the next part of this tutorial, we will start to deep dive into Flask and see how we can structure an app like other frameworks do.