6 examples of 'open .py in jupyter notebook' in Python

Every line of 'open .py in jupyter notebook' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Python code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
23def py_to_pynb(filename, skip_if_exists = True):
24 """
25 Convert a given py file into an ipynb file. If skip_if_exists is True, and
26 the ipynb file is already there, then don't do the conversion.
27 """
28 file_name_headless, ext = os.path.splitext(filename)
29 assert filename.endswith('.py')
30 pynb_filename = file_name_headless+'.ipynb'
31 if os.path.exists(pynb_filename) and skip_if_exists:
32 return
33
34 with open(filename, 'r') as f:
35 nb = nbf.read_py(f.read())
36
37 with open(pynb_filename, 'w') as f:
38 nbf.to_notebook_py(nb, )
39 print 'Created Notebook file: %s' % (pynb_filename, )
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

21def show_notebook(fname):
22 """display a short summary of the cells of a notebook"""
23 with io.open(fname, 'r', encoding='utf-8') as f:
24 nb = nbformat.read(f, 4)
25 html = []
26 for cell in nb.cells:
27 html.append("<h4>{0} cell</h4>".format(cell.cell_type))
28 if cell.cell_type == 'code':
29 html.append(highlight(cell.source, lexer, formatter))
30 else:
31 html.append("<pre>{0}</pre>".format(cell.source))
32 display(HTML('\n'.join(html)))
296def init_code(self):
297 """run the pre-flight code, specified via exec_lines"""
298 self._run_startup_files()
299 self._run_exec_lines()
300 self._run_exec_files()
301
302 # Hide variables defined here from %who etc.
303 if self.hide_initial_ns:
304 self.shell.user_ns_hidden.update(self.shell.user_ns)
305
306 # command-line execution (ipython -i script.py, ipython -m module)
307 # should *not* be excluded from %whos
308 self._run_cmd_line_code()
309 self._run_module()
310
311 # flush output, so itwon't be attached to the first cell
312 sys.stdout.flush()
313 sys.stderr.flush()
111def run(self, **options):
112 """
113 Runs the shell. If no_bpython is False or use_bpython is True, then
114 a BPython shell is run (if installed). Else, if no_ipython is False or
115 use_python is True then a IPython shell is run (if installed).
116
117 :param options: defined in ``self.get_options``.
118 """
119 self.setup_sql_printing(**options)
120 self.setup_pythonrc(**options)
121 self.try_setuping_sa()
122
123 vi_mode = options['vi_mode']
124
125 for key in ('notebook', 'plain', 'bpython', 'ptpython', 'ptipython', 'ipython'):
126 if options.get(key):
127 shell = key
128 break
129 else:
130 shell = get_available_shell()
131
132 self.setup_imports(**options)
133 context = self.context
134
135 if shell == 'notebook':
136 no_browser = options['no_browser']
137 notebook = get_notebook()
138 notebook(no_browser=no_browser, display_name=self.banner)
139 elif shell == 'bpython':
140 from bpython import embed
141 embed(banner=self.banner, locals_=context)
142 elif shell == 'ptpython':
143 from ptpython.repl import embed
144 embed(banner=self.banner, user_ns=context, vi_mode=vi_mode)
145 elif shell == 'ptipython':
146 from ptpython.ipython import embed
147 embed(user_ns=context, vi_mode=vi_mode)
148 elif shell == 'ipython':
149 from IPython import embed
150 embed(user_ns=context)
151 else:
152 # Use basic python shell
153 import code
154 code.interact(self.banner, local=context)
176def start_jupyter(s):
177 global webbrowser_started
178
179 if not webbrowser_started:
180 m = re.search('http://localhost:(?P<url>.*)',s.decode('utf8'))
181 if m is not None:
182 params.update(m.groupdict())
183 if not (OS_c == 'Darwin'):
184 # Open browser locally
185 webbrowser.open('http://localhost:'+params['url'])
186 webbrowser_started = True
187 else:
188 print('using appscript',params['url'])
189 safari = appscript.app("Safari")
190 safari.make(new=appscript.k.document, with_properties={
191 appscript.k.URL: 'http://localhost:'+params['url']})
192
193 webbrowser_started = True
194 return s
62def _in_ipynb():
63 """Check if we are running in a Jupyter Notebook
64
65 Credit to https://stackoverflow.com/a/24937408/3996580
66 """
67 __VALID_NOTEBOOKS = ["<class 'google.colab._shell.Shell'>",
68 "<class 'ipykernel.zmqshell.ZMQInteractiveShell'>"]
69 try:
70 return str(type(get_ipython())) in __VALID_NOTEBOOKS
71 except NameError:
72 return False

Related snippets