https://github.com/emanuelefavero/flask
This is a cheat sheet repo for Flask (Python web framework)
https://github.com/emanuelefavero/flask
flask python server wsgi
Last synced: about 2 months ago
JSON representation
This is a cheat sheet repo for Flask (Python web framework)
- Host: GitHub
- URL: https://github.com/emanuelefavero/flask
- Owner: emanuelefavero
- License: mit
- Created: 2023-02-21T02:12:13.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2023-02-21T05:58:43.000Z (over 3 years ago)
- Last Synced: 2025-02-04T16:50:32.590Z (over 1 year ago)
- Topics: flask, python, server, wsgi
- Language: CSS
- Homepage:
- Size: 14.6 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# Flask
This is a cheat sheet repo for [Flask](https://flask.palletsprojects.com/en/2.2.x/)
> Flask is a lightweight Python [WSGI](https://en.wikipedia.org/wiki/Web_Server_Gateway_Interface?useskin=vector) (Web Server Gateway Interface) web application framework. It is similar to Express.js in Node.js
## Installation for both new projects and cloned repos
- Create a virtual environment
```bash
mkdir myproject
cd myproject
python3 -m venv venv
```
> use `py -3 -m venv venv` on Windows
- Activate the virtual environment
```bash
. venv/bin/activate
```
> use `venv\Scripts\activate` on Windows
- Install Flask
```bash
pip install Flask
```
## Quickstart
- Create a file called `app.py` with the following content:
```python
from flask import Flask
app = Flask(__name__)
@app.route("/")
def hello_world():
return "
Hello, World!
"
```
- Run the app
```bash
flask --app app run --debug
```
> To allow access to other devices on the network, use `--host=0.0.0.0`
## Pass variables
```python
...
def hello_name():
name = 'John'
return f"Hello {name}!"
```
## HTTP Methods
```python
# HTTP METHODS
@app.route("/login", methods=["GET", "POST"])
def login():
if request.method == "POST":
# DO THE LOGIN STUFF
return "You are now logged in"
else:
# SHOW THE LOGIN FORM
return "Please log in"
```
## URL Parameters
```python
# URL PARAMETERS
# specify the type using converters:
# converters: int, float, path, uuid
@app.route("/user/")
def user(name):
return f"Hello {name}"
```
## Static files
- Add a folder called static to the root of the project
- Add a file called `style.css` to the static folder
- Reference that file in the HTML
```html
```
## Template Engine (Jinja2)
- Create a folder called `templates` in the root of the project
- Create a file called `index.html` in the templates folder
- Render the template in the view
```python
from flask import render_template
@app.route("/")
def index():
name = "John"
return render_template("index.html", title="Home", name=name)
```
## Link between pages
```html
Home
```
> Note: `index` is the name of the view function
## Resources
- [Flask Documentation](https://flask.palletsprojects.com/en/2.2.x/)
- [WSGI](https://en.wikipedia.org/wiki/Web_Server_Gateway_Interface?useskin=vector)
- [Python](https://www.w3schools.com/python/)
## License
- [MIT](LICENSE.md)