Every line of 'convert ipynb to py' 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.
23 def 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, )
126 @requires_sphinx_gallery 127 @pytest.mark.parametrize('nb_file', list_notebooks('ipynb_py', skip='(raw|hash|frozen|magic|html|164|long)')) 128 def test_ipynb_to_python_sphinx(nb_file, no_jupytext_version_number): 129 assert_conversion_same_as_mirror(nb_file, 'py:sphinx', 'ipynb_to_sphinx')
31 def nb_to_rst(nb_path): 32 """convert notebook to restructured text""" 33 exporter = rst.RSTExporter() 34 out, resources = exporter.from_file(open(nb_path)) 35 basename = os.path.splitext(os.path.basename(nb_path))[0] 36 imgdir = basename + '_files' 37 img_prefix = os.path.join(imgdir, basename + '_') 38 resources['metadata']['basename'] = basename 39 resources['metadata']['name'] = basename.replace('_', ' ') 40 resources['metadata']['imgdir'] = imgdir 41 base_url = ('http://nbviewer.jupyter.org/github/Unidata/MetPy/blob/master/' 42 'examples/notebooks/') 43 out_lines = ['`Notebook <%s>`_' % (base_url + os.path.basename(nb_path))] 44 for line in out.split('\n'): 45 if line.startswith('.. image:: '): 46 line = line.replace('output_', img_prefix) 47 out_lines.append(line) 48 out = '\n'.join(out_lines) 49 50 return out, resources
162 def to_cell(self): 163 return nbv.new_code_cell(self._text)
62 def _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 = ["", 68 ""] 69 try: 70 return str(type(get_ipython())) in __VALID_NOTEBOOKS 71 except NameError: 72 return False
149 def tonb(path,destination_path=None): 150 """ Convert NBPy to Noteook 151 """ 152 return Py2NB(path,destination_path).convert()
47 def add_cells_to_nb(nb, new_cell_contents): 48 if type(new_cell_contents)==str: 49 nb['worksheets'][0]['cells'].append(create_code_cell(new_cell_contents)) 50 elif type(new_cell_contents)==list and len(new_cell_contents) > 0: 51 if type(new_cell_contents[0])==str: 52 for new_cell in new_cell_contents: 53 nb['worksheets'][0]['cells'].append(create_code_cell(new_cell)) 54 elif type(new_cell_contents[0])==dict: 55 for new_cell in new_cell_contents: 56 nb['worksheets'][0]['cells'].append(new_cell) 57 elif type(new_cell_contents)==dict: 58 nb['worksheets'][0]['cells'].append(new_cell_contents) 59 60 return nb
28 def upgrade(nb, from_version=1): 29 """Convert a notebook to the v2 format. 30 31 Parameters 32 ---------- 33 nb : NotebookNode 34 The Python representation of the notebook to convert. 35 from_version : int 36 The version of the notebook to convert from. 37 """ 38 if from_version == 1: 39 newnb = new_notebook() 40 ws = new_worksheet() 41 for cell in nb.cells: 42 if cell.cell_type == u'code': 43 newcell = new_code_cell(input=cell.get('code'),prompt_number=cell.get('prompt_number')) 44 elif cell.cell_type == u'text': 45 newcell = new_text_cell(u'markdown',source=cell.get('text')) 46 ws.cells.append(newcell) 47 newnb.worksheets.append(ws) 48 return newnb 49 else: 50 raise ValueError('Cannot convert a notebook from v%s to v2' % from_version)
209 @pytest.mark.parametrize('nb_file', list_notebooks('ipynb_py', skip='')) 210 def test_ipynb_to_python_vscode(nb_file, no_jupytext_version_number): 211 assert_conversion_same_as_mirror(nb_file, {'extension': '.py', 'cell_markers': 'region,endregion'}, 212 'ipynb_to_script_vscode_folding_markers')