def console(context, autoreload=False): "Start ipython console for a site" site = get_site(context) frappe.init(site=site) frappe.connect() frappe.local.lang = frappe.db.get_default("lang") from IPython.terminal.embed import InteractiveShellEmbed from atexit import register register(_console_cleanup) terminal = InteractiveShellEmbed() if autoreload: terminal.extension_manager.load_extension("autoreload") terminal.run_line_magic("autoreload", "2") all_apps = frappe.get_installed_apps() failed_to_import = [] for app in all_apps: try: locals()[app] = __import__(app) except ModuleNotFoundError: failed_to_import.append(app) all_apps.remove(app) print("Apps in this namespace:\n{}".format(", ".join(all_apps))) if failed_to_import: print("\nFailed to import:\n{}".format(", ".join(failed_to_import))) terminal.colors = "neutral" terminal.display_banner = False terminal()
class IPythonDebugger: banner = ">>> Custom IPython Debugger" exit_msg = "Leaving debugger" def __init__(self, args=[]): if args: self.exec_cmd = " ".join(self.args) else: self.exec_cmd = inspect.getframeinfo( inspect.currentframe().f_back).filename cfg = Config() cfg.TerminalInteractiveShell.prompts_class = CustomPrompt self.embedded_shell = InteractiveShellEmbed(config=cfg, banner1=self.banner, exit_msg=self.exit_msg) self.embedded_shell.run_line_magic('matplotlib', 'osx') def ipython_debugger(self): try: get_ipython except NameError: # print('running outside IPython') self.embedded_shell.run_line_magic('run', self.exec_cmd) sys.exit(1) else: # print('running in IPython') return self.embedded_shell
def ipython_shell(frame=0, magics=None): from IPython.terminal.embed import InteractiveShellEmbed ipshell = InteractiveShellEmbed() if magics is not None: for magic in magics: if isinstance(magic, str): magic = (magic, '') ipshell.run_line_magic(*magic) ipshell(stack_depth=frame + 1)
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)
import sys from IPython.terminal.embed import InteractiveShellEmbed ipshell = InteractiveShellEmbed() ipshell.run_line_magic("load", sys.argv[1]) ipshell()
env = dict( Eq = lambda a, b: a == b, Gt = lambda a, b: a > b, Ge = lambda a, b: a >= b, Lt = lambda a, b: a < b, Le = lambda a, b: a <= b, Ne = lambda a, b: a != b, Sqrt = z3.Sqrt) env.update(z_vars) return eval(str(func), env), z_vars def parse(*args, **kw): kw['transformations'] = kw.get('transformations', ()) + _transformations kw['evaluate'] = kw.get('evaluate', True) return parse_expr(*args, **kw) if __name__ == '__main__': from IPython.terminal.embed import InteractiveShellEmbed if _in_ipython: shell = get_ipython() else: shell = InteractiveShellEmbed(banner1='', banner2='') shell.run_line_magic('config', r"PromptManager.in_template = 'SYM[\\#]: '") init_printing() if not _in_ipython: shell()