ipython / ipython/ipython

Embedding IPython - Prompt reflecting nesting level, and banner printing filename and lineno

未关闭
#3,095 2 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看
needs-info
主要语言
Python
星标
16.8k
派生
4.5k
平均合并
1 天 2 小时
30 天内合并 PR
6

描述

When embedding IPython, is it possible to modify the prompt to **display** the nesting level?

Also, how can I have my call to `ipshell()` automatically display the `filename` and `lineno` as soon as it starts (e.g. as part of the banner).

More specifically, I would like to be able execute the following code anytime I call `ipshell()`

``` python
frameinfo = getframeinfo(currentframe())
print "Stopped at:"
print frameinfo.filename, frameinfo.lineno
```

I currently have the following setup:

``` python
from IPython.config.loader import Config
try:
get_ipython
except NameError:
nested = 0
cfg = Config()
prompt_config = cfg.PromptManager
prompt_config.in_template = 'In <\\#>: '
prompt_config.in2_template = ' .\\D.: '
prompt_config.out_template = 'Out<\\#>: '
else:
# print("Running nested copies of IPython.")
print("The prompts for the nested IPythons have been modified")
print(" ")
cfg = Config()
nested = 1

# First import the embed function
from IPython.frontend.terminal.embed import InteractiveShellEmbed
# Now create the IPython shell instance. Put ipshell() anywhere in your code where you want it to open.
ipshell = InteractiveShellEmbed(config=cfg, banner1=banner, exit_msg=exit_msg)

msg = '***Called from top level.\n \
Hit Ctrl-D to exit interpreter and continue program.\n \
Note that if you use %kill_embedded, you can fully deactivate\n \
This embedded instance so it will never turn on again';
```

With the above, I can then call `ipshell(msg)` whenever I want my code to open an IPython shell, but:

**How can I have the prompt reflect the nesting level and have the banner display `filename` and `lineno`?**

贡献指南

打开贡献指南

评估

这个 Issue 还没有评估数据。

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。