简体   繁体   English

如何在 PyCharm 中运行 Flask shell?

[英]How to run flask shell in PyCharm?

I created new flask project in PyCharm but I can't see how to run flask shell in integrated PyCharm python console window.我在 PyCharm 中创建了新的烧瓶项目,但我看不到如何在集成的 PyCharm python 控制台窗口中运行烧瓶外壳。

Name app is not defined when start console:启动控制台时未定义名称应用程序: 启动控制台时未定义名称应用程序

I still can run command "flask shell" in integrated PyCharm terminal but it will start without code completion, hints, syntax checks, etc.我仍然可以在集成的 PyCharm 终端中运行命令“flask shell”,但它将在没有代码完成、提示、语法检查等的情况下启动。

Flask app is defined in terminal: Flask 应用程序在终端中定义:

Flask 应用程序在终端中定义 Is there any way to start flask shell in integrated PyCharm python console?有没有办法在集成的 PyCharm python 控制台中启动 Flask shell?

You can use a combination of creating a request context for the Shell (which flask shell does) and a custom starting script for the PyCharm console .您可以结合使用为 Shell 创建请求上下文flask shell这样做)和为 PyCharm 控制台创建自定义启动脚本

在 PyCharm 首选项中的何处输入启动脚本

# Step 1: acquire a reference to your Flask App instance
import app
app_instance = app.create_app()
# I return app instance from a factory method here, but might be be `app.app` for basic Flask projects

# Step 2: push test request context so that you can do stuff that needs App, like SQLAlchemy
ctx = app_instance.test_request_context()
ctx.push()

Now you should have the benefits of flask shell alongside the code completion and other benefits that IPython Shell provides, all from the default PyCharm Python Shell.现在,您应该拥有flask shell的好处以及代码完成和 IPython Shell 提供的其他好处,所有这些都来自默认的 PyCharm Python Shell。

In my case (in my server.py I had app=Flask(__name__) ) I used就我而言(在我的server.py我使用了app=Flask(__name__)

from server import app
ctx = app.app_context()
ctx.push()

Then added this to File | Settings | Build, Execution, Deployment | Console | Python Console然后将其添加到File | Settings | Build, Execution, Deployment | Console | Python Console File | Settings | Build, Execution, Deployment | Console | Python Console File | Settings | Build, Execution, Deployment | Console | Python Console starting script File | Settings | Build, Execution, Deployment | Console | Python Console启动脚本

This also works:这也有效:

from app import app
ctx = app.test_request_context()
ctx.push()

more on the Flask docs page更多关于Flask 文档页面

Running a Shell运行外壳

To run an interactive Python shell you can use the shell command:要运行交互式 Python shell,您可以使用 shell 命令:

 flask shell

This will start up an interactive Python shell, setup the correct application context and setup the local variables in the shell.这将启动一个交互式 Python shell,设置正确的应用程序上下文并在 shell 中设置局部变量。 This is done by invoking the Flask.make_shell_context() method of the application.这是通过调用应用程序的 Flask.make_shell_context() 方法来完成的。 By default you have access to your app and g.默认情况下,您可以访问您的应用程序和 g。

It's just a normal interactive Python shell with some local variables already setup for you, not an IDE editor with code completion, hints, syntax checks, etc.它只是一个普通的交互式 Python shell,其中已经为您设置了一些局部变量,而不是具有代码完成、提示、语法检查等功能的 IDE 编辑器。

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

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