ipython / ipython/ipython

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

Offen
#3,095 2 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen
needs-info
Vorherrschende Sprache
Python
Sterne
16.8k
Forks
4.5k
Ø Merge
1 T. 2 Std.
Gemergte PRs (30 T.)
6

Beschreibung

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

Beitragsleitfaden

Beitragsleitfaden öffnen

Bewertung

Dieses Issue wurde noch nicht bewertet.

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.