flask.md
Creating a New Flask App
Create the project's directory
mkdir MyNewFlaskApp
cd MyNewFlaskApp
Install Flask
pip install Flask
Create & activate the project's virtual environment
python3 -m venv venv
. venv/bin/activate
Create the directory & file that will hold the app's main Python code
mkdir flaskr
touch __init__.py
Adding an
__init__.py
file to a directory tells Python to treat it as a package rather than a module. Though it can be left empty, it usually contains code used to initialize a package. We can use it to house our main application code.
Basic layout in Python file (e.g. __init__.py
)
__init__.py
)Set environment variables & run the application\
Make these are run from the project's directory, not within the flaskr directory.
About Flask
Note that the
create_app()
function takes the name of a configuration file, which contains the names and values of environmental variables to be used by the Flask application.
About Virtual Environments
Using a python virtual environment in a project is a way to ensure all of project's dependencies (e.g. python version, python packages, etc.) "accompany" it and are met wherever it happens to be run.
To add a virtual environment to a project, cd into the project's directory and run
python3 -m venv venv
Whenever you work on your project, activate its virtual environment first, by running
. venv/bin/activate
About SQLite
SQLite is a serverless relational database. Simply put, it allows you to implement a database in your project without having to run/connect to a separate database server.
It's intended for light use, ideal for a development environment (or in production with light activity).
Adding a UI
Bootstrap
To enable using Bootstrap via CDN: 1. Paste this into your HTML document's header, before any other links to css:
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
Paste these in order near the very of your HTML document, right bofore the
</body>
closing tag:
Most Bootstrap elements are added by creating a
<div>
with theclass
attribute set to one of Bootstrap's predefined classes. For example, adding a Boostrap alert element consists of the following:<div class="alert alert-primary" role="alert">A simple primary alert—check it out!</div>
Icons
To enable use of free FontAwesome icons via CDN, add the following link tag to the header of your HTML document:
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.4.1/css/all.css" integrity="sha384-5sAR7xN1Nv6T6+dT2mhtzEpVJvfS3NScPQTrOxhwjIuvcA67KV2R5Jz6kr4abQsz" crossorigin="anonymous">
Brad Nesbitt 10/26/2018
Last updated
Was this helpful?