简体   繁体   English

如何自动化 cmd 命令来运行 flask 服务器

[英]How do I automate cmd commands to run flask server

Every time I start working on my Flask project I need to run these commands in cmd:每次我开始处理我的 Flask 项目时,我都需要在 cmd 中运行这些命令:

cd myproject
venv\scripts\activate
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?有没有办法制作一个可以执行这些命令的批处理文件?

I think something like this could help you.我认为这样的事情可以帮助你。 Open notepad and write the following:打开notepad并写下以下内容:

@ECHO OFF
CALL venv\scripts\activate
set FLASK_APP=project
set FLASK_ENV=development
flask run --host=0.0.0.0

Save this as run.bat inside your myproject directory.将其保存为myproject目录中的run.bat Then you can call this new script.然后你可以调用这个新脚本。

The following code may help you:以下代码可能对您有所帮助:

create init .py in your app folder (project):在您的应用程序文件夹(项目)中创建init .py:

from flask import Flask

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

    return app

then create run.py然后创建 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.那么您所要做的就是输入 python run.py 并输入 go。 (make sure you're in virtual environment) (确保您在虚拟环境中)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM