Create the basic skeleton of a web app that connects to, queries, and serves data from a relational database.
- Flask
- FlaskSQLAlchemy
- A flask plugin for SQLAlchemy. SQLAlchemy is an Object Relational Mapper (ORM), which means it allows interaction with relational data models using object oriented approaches, like those typically used in python.
- This project uses SQLAlchemy to create, read from, and write to relational databases.
- SQLAlchemy's flexibility will allow for a smooth transition from using a local database to using something like Amazon RDS. All that needs to change is a configuration in the app code (see this blog for more on transition to RDS)
- Jinja for creating HTML templates with Python.
- PyMySQL for connecting to Amazon RDS
- Added view that allows user to input data into the database.
- Relational database
- Data model
- View(s)
- Templates
- HTML
- Static files
- CSS
- JS
-
Create conda environment for new app
conda create -n msiapp python=3
-
Activate environment and add Conda Forge channel (if not in channel list yet)
source activate msiapp
conda config --add channels conda-forge
-
Install required packages
conda install flask
conda install flask-sqlalchemy
-
Create directory structure (see repo)
-
Create a config file with the information necessary to create your database connection. Do not commit this file. You never want to commit files with keys or passwords. The file should look like this:
SECRET_KEY = 'development_key' SQLALCHEMY_DATABASE_URI = 'sqlite:////tmp/tracks.db' SQLALCHEMY_TRACK_MODIFICATIONS = True
The
SECRET_KEY
should be a unique, arbitrary string specific to the application that should be random/difficult to guess - "development_key" is just an example.When you create your RDS database, you will change these parameters.
-
msiapp/__init__.py
includes the line of code:application.config.from_envvar('MSIA_SETTINGS', silent=True)
which tells the application to look at the environmental variable
MSIA_SETTINGS
for the path to your config file. This means you need to set this environmental variable yourself by going to command line and entering:export MSIA_SETTINGS="path/to/where/your/config/file/is.config
-
Define data model (see
msiapp/models.py
) - this can be a placeholder and change as the students better define their project's data needs -
Create local table with data model and insert some data (see
create_db.py
) -
Write html template for main page that displays (all or some of the )data from the table in some way.
Flask uses
jinja2
syntax for templates. In it's most basic application, the template is a normal html page but with{{ datafield }}
in the locations where data from the table is wanted. See
msiapp/templates/index.html
for a very basic example -
Create a view (see
application.py
) that presents data from the table through html rendered from themsiapp/templates/index.html
template.A view separates the way in which a user interacts with information and the the way that it is manipulated by the app. The "user" in this case can be an actual user interacting through a web page, or it could be a remote application interacting with it through web services.
-
Get up application running
If you haven't executed
create_db()
and created your database, from the command line:python create_db.py
Now run your application:
python application.py
You should be able to go to the IP address that it responds with and see your web app
-
(Optional for now) Add some basic formatting via a CSS file (see
msiapp/static/basic.css
)