Skip to content

Instantly share code, notes, and snippets.

@xuru
Created March 5, 2013 16:02
Show Gist options
  • Select an option

  • Save xuru/5091360 to your computer and use it in GitHub Desktop.

Select an option

Save xuru/5091360 to your computer and use it in GitHub Desktop.
Embed ipython when using a cmd.Cmd (or kmd.Kmd) shell
from IPython.core.completer import IPCompleter
from IPython.config.loader import Config
import rl
def setup_ipython():
try:
get_ipython()
except NameError:
cfg = Config()
prompt_config = cfg.PromptManager
prompt_config.in_template = 'In <\\#>: '
prompt_config.in2_template = ' .\\D.: '
prompt_config.out_template = 'Out<\\#>: '
banner = "** Dropping into IPython **"
exit_msg = "** Exiting IPython **"
from IPython.frontend.terminal.embed import InteractiveShellEmbed
ipshell = InteractiveShellEmbed(config=cfg, banner1=banner, exit_msg=exit_msg)
rl.readline.set_completer(IPCompleter(shell=ipshell).complete)
rl.readline.parse_and_bind("tab: complete")
return ipshell
# Now ipshell() will open IPython anywhere in the code
else:
# Define a dummy ipshell() so the same code doesn't crash inside an
# interactive IPython
def ipshell():
pass
return ipshell
class BaseCmd(kmd.Kmd):
# other stuff omitted...
def do_ipy(self, arg):
'Enter the development shell'
"""Run the ipython shell (developer only)"""
ipshell = setup_ipython()
ipshell('***Called from top level. '
'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')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment