From 78d061ced5a835554d6fca3e578996f52cbe29d6 Mon Sep 17 00:00:00 2001 From: Himanshu Mishra Date: Wed, 22 Aug 2018 11:03:08 +0530 Subject: [PATCH] [feature] bench jupyter command (#5983) * [feature]: bench jupyter * fix: sites path in init * fix: handle OSError instead of all --- frappe/commands/utils.py | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/frappe/commands/utils.py b/frappe/commands/utils.py index 3e27575418..c6cddf54f8 100644 --- a/frappe/commands/utils.py +++ b/frappe/commands/utils.py @@ -315,6 +315,45 @@ def mariadb(context): '--pager=less -SFX', "-A"]) +@click.command('jupyter') +@pass_context +def jupyter(context): + try: + from pip import main + except ImportError: + from pip._internal import main + + reqs = subprocess.check_output([sys.executable, '-m', 'pip', 'freeze']) + installed_packages = [r.decode().split('==')[0] for r in reqs.split()] + if 'jupyter' not in installed_packages: + main(['install', 'jupyter']) + site = get_site(context) + frappe.init(site=site) + jupyter_notebooks_path = os.path.abspath(frappe.get_site_path('jupyter_notebooks')) + sites_path = os.path.abspath(frappe.get_site_path('..')) + try: + os.stat(jupyter_notebooks_path) + except OSError: + print('Creating folder to keep jupyter notebooks at {}'.format(jupyter_notebooks_path)) + os.mkdir(jupyter_notebooks_path) + bin_path = os.path.abspath('../env/bin') + print(''' +Stating Jupyter notebook +Run the following in your first cell to connect notebook to frappe +``` +import frappe +frappe.init(site='{site}', sites_path='{sites_path}') +frappe.connect() +frappe.local.lang = frappe.db.get_default('lang') +frappe.db.connect() +``` + '''.format(site=site, sites_path=sites_path)) + os.execv('{0}/jupyter'.format(bin_path), [ + '{0}/jupyter'.format(bin_path), + 'notebook', + jupyter_notebooks_path, + ]) + @click.command('console') @pass_context def console(context): @@ -610,6 +649,7 @@ commands = [ build, clear_cache, clear_website_cache, + jupyter, console, destroy_all_sessions, execute,