ipython / ipython/ipython

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

Open
#3,095 2 comments 0 reactions 0 assignees View on GitHub
needs-info
Dominant language
Python
Stars
16.8k
Forks
4.5k
Avg merge
1d 2h
Merged PRs (30d)
6

Description

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`?**

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.