Lately I was creating a loot of flask apps and I was getting a bit annoyed typing each time all the commands (make venv, flask env, pip installing, gitignore...)
So I decided to make a package that I can install with pip and add a command that will make all the files I need and install the packages I want in the venv.
This is the code I came up with: repo for the code - more code there.
import subprocess
import secrets
import os
import pathlib
#Path for where the script is runing
path = os.getcwd()
#Path for this file
this_path = pathlib.Path(__file__).parent.resolve()
def create():
subprocess.run("python -m venv .venv")
subprocess.run("mkdir Routes")
subprocess.run("mkdir Modules")
subprocess.run("touch main.py .gitignore .env .flaskenv requirements.txt .venv/scripts/activate_this.py")
with open(f"{path}\\.venv\\scripts\\activate_this.py", "w") as venv_activator:
venv_activator.write(activate_this)
# Gitignore write
with open(f"{path}\\.gitignore", "w") as git:
git.write(".venv \n__pycache__ ")
# Basic fleskenv file
with open(f"{path}\\.flaskenv", "w") as flask_env:
flask_env.write("FLASK_APP=main.py\nFLASK_ENV=development\nDEBUG=True")
# Creates env file with random secret key
with open(f"{path}\\.env", "w") as env:
env.write(f"SECRET_KEY={secrets.token_hex(48)}\n")
# Basic flask app
with open(f"{path}\\main.py", "w") as main:
main.write('from flask import Flask\nfrom dotenv import load_dotenv\nimport os\n\n\nload_dotenv()\n\napp = Flask(__name__)\napp.config["SECRET_KEY"] = os.getenv("SECRET_KEY")')
activator = f"{path}\\.venv\\scripts\\activate_this.py"
with open(activator) as f:
exec(f.read(), {'__file__': activator})
subprocess.run("pip install flask python-dotenv")
with open(f"{path}\\requirements.txt", "w") as requirements:
subprocess.run("pip3 freeze > requirements.txt", stdout=requirements)
There is some stuff that needs improvement, like how I create the files. If you got any ideas on how to make improvements I will appreciate it!
You can install the package with pip install basic-flask
and then in the directory You want to create the flask app run the commend: create-flask