Example #1
0
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()