1 Star 0 Fork 0

burnc/createFlask

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
cf.py 3.85 KB
一键复制 编辑 原始数据 按行查看 历史
burnc 提交于 2020-10-23 21:45 . init
#!/usr/bin/python3
#coding=utf-8
print("程序启动")
import time
t = time.time()
import os
import sys
args = sys.argv
if len(args) <= 2 or args[0] == "--help":
print("描述:创建一个flask项目\n用法:cf.py [项目名] [路径]")
sys.exit(0)
path = args[2]
name = args[1]
print(f"在{path}目录下创建名为{name}的flask项目")
def check():
if not os.path.isdir(path):
print(f"{path}不是一个目录!")
sys.exit(1)
if os.listdir(os.path.abspath(path)):
input("警告:目录非空,可能会覆盖文件或发生错误,回车表示继续。")
print("基本检查...")
check()
path = os.path.abspath(path)
os.chdir(path)
print("依赖检查...")
try:
import flask
import python_dotenv
import flask_sqlalchemy
import flask_debugtoolbar
import flask_caching
except ModuleNotFoundError:
print("有依赖未安装,自动安装!")
if os.system(f"{sys.executable} -m pip install flask\
python-dotenv flask_sqlalchemy flask_debugtoolbar flask_caching") == 0:
print("完成")
else:
print("安装时可能出现了问题,程序结束后请手动解决")
print('.env')
f = open(".env", "w")
f.writelines(['SECRET_KEY="<xxx>"'])
f.close()
print('.flaskenv')
f = open(".flaskenv", "w")
f.writelines(["FLASK_ENV=development\n", f"FLASK_APP={name}"])
f.close()
print("README.md")
f = open("README.md", "w")
f.writelines([f"# {name}"])
f.close()
print('.gitignore')
f = open(".gitignore", "w")
f.write('.env \n__pycache__ \n*.pyc')
f.close()
print(name)
os.mkdir(name)
os.chdir(name)
print(f"cd {name}")
files = {
"settings.py": f"""\
#-*- 配置文件 -*-#
import os
import sys
from {name} import app
WIN = sys.platform.startswith('win')
if WIN: # 如果是 Windows 系统,使用三个斜线
prefix = 'sqlite:///'
else: # 否则使用四个斜线
prefix = 'sqlite:////'
SQLALCHEMY_DATABASE_URI = prefix + os.path.join(
app.root_path, 'data.db'
)
# 关闭对模型修改的监控
SQLALCHEMY_TRACK_MODIFICATIONS = False
# 密钥
SECRET_KEY = os.getenv("SECRET_KEY")
# 缓存类型
# 有"null"(无缓存), "simple"(py字典), "filesystem", "redis"等
CACHE_TYPE = "null"
CACHE_DIR = os.path.join(os.path.dirname(app.root_path), 'cache')
CACHE_DEFAULT_TIMEOUT = 20*60
#-*- 仅用于开发 -*-#
DEBUG_TB_INTER_CEPT_REDIRECTS = False
""",
"__init__.py": f"""\
#coding:utf-8
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from flask_debugtoolbar import DebugToolbarExtension
from flask_caching import Cache
app = Flask(__name__)
app.config.from_pyfile('settings.py') # 从settings.py中导入设置
# 去除jinja模板中的空白行
app.jinja_env.trim_blocks = True
app.jinja_env.lstrip_blocks = True
db = SQLAlchemy(app)
toolbar = DebugToolbarExtension(app)
cache = Cache(app)
from {name} import views, errors, commands
""",
"views.py": f"""\
from {name} import app, cache
from flask import render_template, abort, request, redirect, flash
@app.route('/')
def index():
return 'Hello world!'
""",
"forms.py": """\
from flask_wtf import FlaskForm
from wtforms import (
Form,
StringField,
PasswordField,
BooleanField,
SubmitField,
)
from wtforms.validators import (
DataRequired,
Length,
InputRequired,
)
""",
"models.py": f"""\
from {name} import db
""",
"commands.py": f"""\
import click
""",
"errors.py": f"""\
from {name} import app
from flask import abort, render_template
# 玩笑..
@app.route('/brew/coffee')
def brew_coffee():
abort(418)
"""
}
for n, c in files.items():
print(n)
f = open(n, "w")
f.write(c)
f.close()
os.mkdir("templates")
os.mkdir("static")
print("git? [y/n]", end="")
x = input()
if x == "y" or x == "":
os.chdir(path)
os.system('git init && git add . && git commit -m "init"')
print("done!")
print(f"{time.time()-t}" + "s")
print("程序结束")
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Python
1
https://gitee.com/gitkunkun/create-flask.git
git@gitee.com:gitkunkun/create-flask.git
gitkunkun
create-flask
createFlask
master

搜索帮助

0d507c66 1850385 C8b1a773 1850385