|
| 1 | +# Flask Series: Sample Project |
| 2 | + |
| 3 | +This repository contains the code for the `board` Flask sample project. |
| 4 | + |
| 5 | +## Setup |
| 6 | + |
| 7 | +You can run the provided example project on your local machine by following the steps outlined below. |
| 8 | + |
| 9 | +Create a new virtual environment: |
| 10 | + |
| 11 | +```bash |
| 12 | +python3 -m venv venv/ |
| 13 | +``` |
| 14 | + |
| 15 | +Activate the virtual environment: |
| 16 | + |
| 17 | +```bash |
| 18 | +source ./venv/bin/activate |
| 19 | +``` |
| 20 | + |
| 21 | +Navigate to the folder for the step that you're currently on. |
| 22 | + |
| 23 | +Install the dependencies for this project if you haven't installed them yet: |
| 24 | + |
| 25 | +```bash |
| 26 | +(venv) $ python -m pip install -r requirements.txt |
| 27 | +``` |
| 28 | + |
| 29 | +### Environment Variables |
| 30 | + |
| 31 | +This project works with environment variables that the application expects in a `.env` file inside the root directory of your project. |
| 32 | + |
| 33 | +Create a `.env` file with this content: |
| 34 | + |
| 35 | +``` |
| 36 | +FLASK_SECRET_KEY="mysecretkey" |
| 37 | +FLASK_DATABASE="board.sqlite" |
| 38 | +``` |
| 39 | + |
| 40 | +You can add your own content there, but you must define it before running the Flask application. |
| 41 | + |
| 42 | +#### Secret Key |
| 43 | + |
| 44 | +If you want to deploy your Flask app later, then it's a good idea to generate a proper secret key. |
| 45 | + |
| 46 | +If you need to create cryptographically sound data like a Flask secret key, then you can use Python's [`secrets`](https://docs.python.org/3/library/secrets.html) module: |
| 47 | + |
| 48 | +```pycon |
| 49 | +>>> import secrets |
| 50 | +>>> secrets.token_hex() |
| 51 | +'2e9ac41b1e0b66a8d93d66400e2300c4b4c2953f' |
| 52 | +``` |
| 53 | + |
| 54 | +The `.token_hex()` method returns a [hexadecimal](https://en.wikipedia.org/wiki/Hexadecimal) string containing random numbers and letters from `0` to `9` and `a` to `f`. Use the value that `secrets.token_hex()` outputs for you and add it to your Flask project's `.env` file: |
| 55 | + |
| 56 | +``` |
| 57 | +# .env |
| 58 | +
|
| 59 | +FLASK_SECRET_KEY="2e9ac41b1e0b66a8d93d66400e2300c4b4c2953f" |
| 60 | +FLASK_DATABASE="board.sqlite" |
| 61 | +
|
| 62 | +``` |
| 63 | + |
| 64 | +To avoid saving the secret key directly in your code, it may be a good idea to work with [environment variables](https://12factor.net/config). You can learn more about that in the Flask documentation on [configuration handling](https://flask.palletsprojects.com/en/2.3.x/config/). |
| 65 | + |
| 66 | +### Database |
| 67 | + |
| 68 | +To initialize the database, run this command: |
| 69 | + |
| 70 | +```bash |
| 71 | +(venv) $ python -m flask --app board init-db |
| 72 | +``` |
| 73 | + |
| 74 | +If you used the content for the `.env` file from above, then you can find a `board.sqlite` database in the root directory of your project. |
| 75 | + |
| 76 | +## Development Server |
| 77 | + |
| 78 | +To run the Flask development server, enter this command in your terminal while being in the root directory of your project: |
| 79 | + |
| 80 | +```bash |
| 81 | +(venv) $ python -m flask --app board run --debug |
| 82 | +``` |
| 83 | + |
| 84 | +Now you can navigate to the address that's shown in the output when you start the server. Commonly, that's `http://localhost:5000/`. |
0 commit comments