【问题标题】:How to gracefully pre-process code in IPython?如何优雅地预处理 IPython 中的代码?
【发布时间】:2019-08-27 14:26:03
【问题描述】:

主要问题是 - 如何以完美的方式对 IPython 控制台中的行输入进行自定义处理?

IPython 控制台嵌入在我的 Python 应用程序中。 在我的应用程序中,我有旧版本的控制台,基于code.InteractiveConsoleInteractiveConsole.raw_input 方法中有一个处理,它可以工作。 IPython 中的类比在哪里?

挖掘文档,我发现input_transformers_cleanup 看起来很适合我的目标。但是如何使用呢?我的调用 get_ipython() 返回 None 我不确定这是正确的方法。

谢谢。

【问题讨论】:

  • 通过the sources 挖掘通常会有很大帮助。
  • 对不起,答案没有提供信息。该模块用于表单基本输入。但是,也许,我必须重写 'run_cell' 方法?我该怎么做?
  • 这不是一个正确的答案,只是我链接了可能有用的链接。

标签: python ipython


【解决方案1】:

好的,这是我的模式:

import IPython
# IPython.start_ipython(config=c)

from traitlets import Type
from IPython.terminal.interactiveshell import TerminalInteractiveShell

class MyTerminalInteractiveShell(TerminalInteractiveShell):
    def run_cell(self, raw_cell, store_history=False, silent=False, shell_futures=True):
        print("Override successful!!!", raw_cell)
        super(MyTerminalInteractiveShell, self).run_cell(raw_cell, store_history, silent, shell_futures)

class MyTerminalIPythonApp(IPython.terminal.ipapp.TerminalIPythonApp):
    interactive_shell_class = Type(
        klass=object,   # use default_value otherwise which only allow subclasses.
        default_value=MyTerminalInteractiveShell,
        help="Class to use to instantiate the TerminalInteractiveShell object. Useful for custom Frontends"
    ).tag(config=True)

app_class = MyTerminalIPythonApp
app = app_class.instance(config=c)
app.initialize()
app.start()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-11-26
    • 1970-01-01
    • 1970-01-01
    • 2011-08-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多