def __run_ipython_magic(self, magic, params='', filter_out=None): from IPython.terminal.embed import InteractiveShellEmbed ipshell = InteractiveShellEmbed() ipshell.dummy_mode = False try: print("%{} {}".format(magic, params)) o = ipshell.run_line_magic(magic, params) except Exception as e: print( "Failed to run IPython magic: %{} with parameters: {}".format( magic, params)) raise e # Some commands output directly (e.g. %time), some return the output (e.g. %sx) - it's weird if (o): for l in o: if filter_out: if re.search(filter_out, l): continue print(l)
ipshell('***In foo(). Try %whos, or print s or m:') print('foo says m = ',m) def bar(n): s = 'eggs' ipshell('***In bar(). Try %whos, or print s or n:') print('bar says n = ',n) # Some calls to the above functions which will trigger IPython: print('Main program calling foo("eggs")\n') foo('eggs') # The shell can be put in 'dummy' mode where calls to it silently return. This # allows you, for example, to globally turn off debugging for a program with a # single call. ipshell.dummy_mode = True print('\nTrying to call IPython which is now "dummy":') ipshell() print('Nothing happened...') # The global 'dummy' mode can still be overridden for a single call print('\nOverriding dummy mode manually:') ipshell(dummy=False) # Reactivate the IPython shell ipshell.dummy_mode = False print('You can even have multiple embedded instances:') ipshell2() print('\nMain program calling bar("spam")\n') bar('spam')
from IPython.terminal.embed import InteractiveShellEmbed ipshell = InteractiveShellEmbed() ipshell.dummy_mode = True import os import numpy as np print('\nInstall Trax:') ipshell.magic("%pip install -q -U trax") import trax
def bar(n): s = 'eggs' ipshell('***In bar(). Try %whos, or print s or n:') print('bar says n = ', n) # Some calls to the above functions which will trigger IPython: print('Main program calling foo("eggs")\n') foo('eggs') # The shell can be put in 'dummy' mode where calls to it silently return. This # allows you, for example, to globally turn off debugging for a program with a # single call. ipshell.dummy_mode = True print('\nTrying to call IPython which is now "dummy":') ipshell() print('Nothing happened...') # The global 'dummy' mode can still be overridden for a single call print('\nOverriding dummy mode manually:') ipshell(dummy=False) # Reactivate the IPython shell ipshell.dummy_mode = False print('You can even have multiple embedded instances:') ipshell2() print('\nMain program calling bar("spam")\n') bar('spam')