如何在传递自定义用户和横幅的同时启动ipython应用程序实例?

2024-04-25 22:02:29 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试修复一个bugin flask脚本。Django曾经有一个similar bug,IPython开发人员建议使用TerminalIPythonApp而不是embed(),以便正确加载扩展。但是,djangoshell不允许像flask脚本那样定制横幅或名称空间。在

用户和横幅通常传递给shell的构造函数,但是TerminalIPythonApp类实例化它自己的shell,而不公开任何允许我们传递shell init参数的kwargs。在

我想出了下面的解决方案,不过这有点老套。有更好的方法吗?在

from IPython.terminal import ipapp
app = ipapp.TerminalIPythonApp.instance()
shell = ipapp.TerminalInteractiveShell.instance(
    parent=app,
    display_banner=False,
    profile_dir=app.profile_dir,
    ipython_dir=app.ipython_dir,
    user_ns=my_user_ns,
    banner1=my_banner)
shell.configurables.append(app)
app.shell = shell
# shell has already been initialized, so we have to monkeypatch
# app.init_shell() to act as no-op
app.init_shell = lambda: None
app.initialize(argv=[])
app.start()

Tags: instance脚本appflaskinitdiripythonshell
1条回答
网友
1楼 · 发布于 2024-04-25 22:02:29

在新的ipython1.0版本中,有一个顶级的IPython.start_ipython()函数来启动应用程序。这正是您想要的,但不幸的是,我们在1.0版本中出错了,并且将user_ns传递给它目前没有任何效果。我们可能会在几周内发布一个错误修复版本,然后鼓励人们尽可能开始使用新功能。在

相关问题 更多 >

    热门问题