Coverage for src/pymyinstall/win_installer/win_ipython_helper.py: 36%
42 statements
« prev ^ index » next coverage.py v7.1.0, created at 2023-07-19 01:47 +0200
« prev ^ index » next coverage.py v7.1.0, created at 2023-07-19 01:47 +0200
1# -*- coding: utf-8 -*-
2"""
3@file
4@brief Functions to prepare a setup on Windows
6.. todoext::
7 :title: enables js extension on jupyter
8 :tag: enhancement
10 Run something like::
12 jupyter nbextension enable --py --sys-prefix widgetsnbextension
13"""
14from __future__ import print_function
15import sys
16import os
18from ..installhelper.install_cmd_helper import run_cmd, get_pip_program
19from .win_exception import WinInstallException
21if sys.version_info[0] == 2:
22 FileNotFoundError = Exception
25def ipython_create_profile(config_path, python_path, name="win_profile", fLOG=print):
26 """
27 creates a ipython profile
29 @param config_path where to create it
30 @param python_path python path (to get ipython)
31 @param profile name
32 @param fLOG logging function
33 @return profile path
34 """
35 if sys.platform.startswith("win"):
36 ipython_path = os.path.join(python_path, "ipython.exe")
37 if not os.path.exists(ipython_path):
38 ipython_path = os.path.join(
39 python_path, "Scripts", "ipython.exe")
40 if not os.path.exists(ipython_path):
41 raise FileNotFoundError(ipython_path)
42 else:
43 ipython_path = os.path.join(
44 python_path, "ipython")
46 cmd = " profile create {1} --ipython-dir={0}".format(config_path, name)
47 cmd = ipython_path + cmd
48 out, err = run_cmd(cmd, wait=True, fLOG=fLOG, change_path=python_path)
49 profile = os.path.join(
50 config_path, "profile_" + name, "ipython_config.py")
51 if not os.path.exists(profile):
52 raise WinInstallException(
53 "missing file, unable to execute:\nFILE:\n{3}\nCMD:\n{0}\nOUT:\n{1}\nERR-6:\n{2}".format(cmd, out, err, profile))
54 return os.path.dirname(profile)
57def ipython_update_profile(profile_path):
58 """
59 update the profile with custom settings (file filters)
61 @param profile_path path to profile
62 """
63 profile = os.path.join(profile_path, "ipython_kernel_config.py")
64 with open(profile, "r") as f:
65 content = f.read()
66 add = """
67 c.ContentsManager.hide_globs = ['__pycache__', '*.pyc', '*.pyo', '.DS_Store', '*.so', '*.dylib', '*~', ".ipynb_checkpoints",
68 ".kernel-*.json", ".kernel", ".RData", ".RHistory"]
69 c.FileContentsManager.hide_globs = ['__pycache__', '*.pyc', '*.pyo', '.DS_Store',
70 '*.so', '*.dylib', '*~', ".ipynb_checkpoints", ".kernel-*.json", ".kernel", ".RData", ".RHistory"]
71 """.split("\n")
72 content += "\n".join(_.strip() for _ in add)
73 with open(profile, "w") as f:
74 f.write("\n" + content + "\n")
77def install_jupyter_extension(python_path):
78 """
79 install jupyter extension
81 @param python_path python path
82 @return out, err
83 """
84 if sys.platform.startswith("win"):
85 pip = os.path.join(python_path, "Scripts", "pip.exe")
86 if not os.path.exists(pip):
87 pip = get_pip_program(exe=python_path)
88 else:
89 pip = get_pip_program(exe=python_path)
91 cmd = "{0} install https://github.com/ipython-contrib/IPython-notebook-extensions/archive/master.zip --user".format(
92 pip)
93 out, err = run_cmd(cmd)
94 if err:
95 raise WinInstallException(
96 "unable to install jupyter extension\nOUT:{0}\nERR-7{1}".format(out, err))
97 return out, err