How do I automate cmd commands to run flask server

Question:

Every time I start working on my Flask project I need to run these commands in cmd:

cd myproject
venvscriptsactivate
set FLASK_APP=project
set FLASK_ENV=development
flask run --host=0.0.0.0

Is there a way to make a batch file that would execute these commands?

Asked By: iheb salah

||

Answers:

I think something like this could help you.
Open notepad and write the following:

@ECHO OFF
CALL venvscriptsactivate
set FLASK_APP=project
set FLASK_ENV=development
flask run --host=0.0.0.0

Save this as run.bat inside your myproject directory.

Then, if you still want to interact with the bat file after your execution you can call this new script, in the following way:

cmd /k pathtomyprojectrun.bat
Answered By: Filippo Lauria

The following code may help you:

create init.py in your app folder (project):

from flask import Flask

def create_app():
    app = Flask(__name__)
    # configure the app here

    return app

then create run.py

from project import create_app

app = create_app()
if __name__ == "__main__":
    app.run(host="0.0.0.0", port="5000", debug=True, use_reloader=True)

then all you have to do is type python run.py an there you go. (make sure you’re in virtual environment)

Answered By: Elie Saad
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.