In order to use session in flask you need to set the secret key in your application settings.
secret key is a random key used to encrypt your cookies and save send them to the browser.
Here is a way to store Flask's secret_key in a hidden file instead of the code:
import secrets
from pathlib import Path
from flask import Flask
app = Flask(__name__)
SECRET_FILE_PATH = Path(".flask_secret")
try:
with SECRET_FILE_PATH.open("r") as secret_file:
app.secret_key = secret_file.read()
except FileNotFoundError:
# Let's create a cryptographically secure code in that file
with SECRET_FILE_PATH.open("w") as secret_file:
app.secret_key = secrets.token_hex(32)
secret_file.write(app.secret_key)
It's always a good idea to store secrets away from versioned code. Git is very good at not losing data. This includes secret keys and passwords :)