0
\$\begingroup\$

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

\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

os.getcwd() can (and should) be replaced with a similar call in pathlib.

subprocess.run should in all instances be replaced with check_call; though using subprocess in the following cases is not justified:

  • the mkdir subprocess call should go away entirely and instead use the Python-native mkdir.
  • python -m venv should be replaced with calls to the API
  • touch should be replaced with Path.touch

Unfortunately the same cannot be said for pip and pip3: The authors do not recommend using it as an API, so you're stuck with subprocess.check_call.

Does this code run? activate_this seems undefined.

Writing to main.py the way you do is questionable. At the very least, you should use a multi-line docstring; but it would be better to hold main.py on the filesystem in your module and copy to the destination.

\$\endgroup\$
1
  • 1
    \$\begingroup\$ Thanks for the answer activate_this is around about 40 lines so I didn't include it, should have noted that. I changed the main.py and activate_this to be files that I copy with shutil copy function instead of writing directly to them. And about all the other comments I will try to implement them. and yes it is working as it should with how the code is right now (the updated version is in the git repo) and thanks again for investing your time to review my code! \$\endgroup\$
    – omercotkd
    Commented Apr 10, 2022 at 22:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.