合规国际互联网加速 OSASE为企业客户提供高速稳定SD-WAN国际加速解决方案。 广告
# flask\_script插件的使用 作用就是通过命令行来操作flask 新建一个python文件 名字manage.py来存放配置 简单测试 ``` <pre class="calibre13">``` <span class="token2">from</span> flask_script <span class="token2">import</span> Manager #导入app <span class="token2">from</span> app <span class="token2">import</span> app manage <span class="token">=</span> <span class="token3">Manager</span><span class="token1">(</span>app<span class="token1">)</span> #写命令 #首先设置装饰器 @manage<span class="token1">.</span>command def <span class="token3">runserver</span><span class="token1">(</span><span class="token1">)</span><span class="token1">:</span> <span class="token3">print</span><span class="token1">(</span><span class="token4">'服务器已经启动'</span><span class="token1">)</span> <span class="token2">if</span> __name__ <span class="token">==</span> <span class="token4">'__main__'</span><span class="token1">:</span> manage<span class="token1">.</span><span class="token3">run</span><span class="token1">(</span><span class="token1">)</span> ``` ``` 在虚拟环境命令行输入 python [manage.py](http://manage.py) runserver 即可激活函数runserver 与数据库相关的命令重新创建一个文件db\_script.py不是作为主文件 是让manage.py引用 ``` <pre class="calibre13">``` <span class="token2">from</span> flask_script <span class="token2">import</span> Manager DBManager <span class="token">=</span> <span class="token3">Manager</span><span class="token1">(</span><span class="token1">)</span>#因为是为了让manage<span class="token1">.</span>py引用不是作为主app 所以可以不用引入app #开始写命令 @DBManager<span class="token1">.</span>command def <span class="token3">init</span><span class="token1">(</span><span class="token1">)</span><span class="token1">:</span> <span class="token3">print</span><span class="token1">(</span><span class="token4">'数据库初始化'</span><span class="token1">)</span> @DBManager<span class="token1">.</span>command def <span class="token3">migrate</span><span class="token1">(</span><span class="token1">)</span><span class="token1">:</span> <span class="token3">print</span><span class="token1">(</span><span class="token4">'数据库迁移'</span><span class="token1">)</span> ``` ``` 在manage.py进行加载 ``` <pre class="calibre14">``` <span class="token2">from</span> flask_script <span class="token2">import</span> Manager #导入app <span class="token2">from</span> app <span class="token2">import</span> app <span class="token2">from</span> db_script <span class="token2">import</span> DBManager manager <span class="token">=</span> <span class="token3">Manager</span><span class="token1">(</span>app<span class="token1">)</span> #写命令 #首先设置装饰器 @manager<span class="token1">.</span>command def <span class="token3">runserver</span><span class="token1">(</span><span class="token1">)</span><span class="token1">:</span> <span class="token3">print</span><span class="token1">(</span><span class="token4">'服务器已经启动'</span><span class="token1">)</span> manager<span class="token1">.</span><span class="token3">add_command</span><span class="token1">(</span><span class="token4">'db'</span><span class="token1">,</span>DBManager<span class="token1">)</span> <span class="token2">if</span> __name__ <span class="token">==</span> <span class="token4">'__main__'</span><span class="token1">:</span> manager<span class="token1">.</span><span class="token3">run</span><span class="token1">(</span><span class="token1">)</span> <span><span class="token4">``</span></span>` ``` ```