def convert_nb(nbname): """ Open up a Jupyter notebook, and clean outputs """ # Remove file extension nbname = os.path.splitext(nbname)[0] os.system(f"runipy --o {nbname}.ipynb --matplotlib --quiet") os.system("rm -rf ./index_files") filename = f"{nbname}.ipynb" with io.open(filename, 'r', encoding='utf8') as f: nb = current.read(f, 'json') nb = clean_for_doc(nb) print("Writing to", filename) with io.open(filename, 'w', encoding='utf8') as f: current.write(nb, f, 'json') os.system(f"jupyter nbconvert --to rst {nbname}.ipynb") filename = f"{nbname}.ipynb" with io.open(filename, 'r', encoding='utf8') as f: nb = current.read(f, 'json') nb = strip_output(nb) print("Writing to", filename) with io.open(filename, 'w', encoding='utf8') as f: current.write(nb, f, 'json')
def load_module(self, fullname): """import a notebook as a module""" path = find_notebook(fullname, self.path) print("importing Jupyter notebook from %s" % path) # load the notebook object with io.open(path, 'r', encoding='utf-8') as f: nb = current.read(f, 'json') # create the module and add it to sys.modules # if name in sys.modules: # return sys.modules[name] mod = types.ModuleType(fullname) mod.__file__ = path mod.__loader__ = self mod.__dict__['get_ipython'] = get_ipython sys.modules[fullname] = mod # extra work to ensure that magics that would affect the user_ns # actually affect the notebook module's ns save_user_ns = self.shell.user_ns self.shell.user_ns = mod.__dict__ try: for cell in nb.worksheets[0].cells: if cell.cell_type == 'code' and cell.language == 'python': # transform the input to executable Python code = self.shell.input_transformer_manager.transform_cell( cell.input) # run the code in themodule exec(code, mod.__dict__) finally: self.shell.user_ns = save_user_ns return mod
def count_words_in_jupyter(filePath: str, returnType: str = "markdown"): with io.open(filePath, "r", encoding="utf-8") as f: nb = current.read(f, "json") word_count_markdown: int = 0 word_count_heading: int = 0 word_count_code: int = 0 for cell in nb.worksheets[0].cells: if cell.cell_type == "markdown": word_count_markdown += len(cell["source"].replace( "#", "").lstrip().split(" ")) elif cell.cell_type == "heading": word_count_heading += len(cell["source"].replace( "#", "").lstrip().split(" ")) elif cell.cell_type == "code": word_count_code += len(cell["input"].replace( "#", "").lstrip().split(" ")) if returnType == "markdown": return word_count_markdown elif returnType == "heading": return word_count_heading elif returnType == "code": return word_count_code else: return Exception
def load_module(self, fullname): """import a notebook as a module""" path = find_notebook(fullname, self.path) print ("importing Jupyter notebook from %s" % path) # load the notebook object with io.open(path, 'r', encoding='utf-8') as f: nb = nbformat.read(f, 'json') # create the module and add it to sys.modules # if name in sys.modules: # return sys.modules[name] mod = types.ModuleType(fullname) mod.__file__ = path mod.__loader__ = self mod.__dict__['get_ipython'] = get_ipython sys.modules[fullname] = mod # extra work to ensure that magics that would affect the user_ns # actually affect the notebook module's ns save_user_ns = self.shell.user_ns self.shell.user_ns = mod.__dict__ try: for cell in nb.worksheets[0].cells: if cell.cell_type == 'code' and cell.language == 'python': # transform the input to executable Python code = self.shell.input_transformer_manager.transform_cell(cell.input) # run the code in themodule exec(code, mod.__dict__) finally: self.shell.user_ns = save_user_ns return mod
def notebook(notebook): """ Dynamically render IPython Notebook """ try: notebook = nbf.read(open('notebooks/%s' % notebook, 'r'), 'ipynb') except IOError: abort(418) html_notebook = convert_nb_html(notebook) return render_template('notebook.html', content=html_notebook)
def adder(): """ Inject input parameters to the adder notebook before rendering """ if request.method == 'POST': notebook = nbf.read(open('notebooks/adder.ipynb', 'r'), 'ipynb') notebook = inject_params(request.form, notebook) html_notebook = convert_nb_html(notebook) return render_template('notebook.html', content=html_notebook) else: params = forms.AdderForm() return render_template('adder.html', form=params)
def process_notebook_file(fname, action='clean', output_fname=None): print("Performing '{}' on: {}".format(action, fname)) orig_wd = os.getcwd() with io.open(fname, 'r') as f: nb = current.read(f, 'json') if action == 'check': os.chdir(os.path.dirname(fname)) run_notebook(nb) remove_outputs(nb) remove_signature(nb) else: # Clean by default remove_outputs(nb) remove_signature(nb) os.chdir(orig_wd) if output_fname is None: output_fname = fname with io.open(output_fname, 'w') as f: nb = current.write(nb, f, 'json')
def read_jupyter(filePath: str, returnType: str): with io.open(filePath, "r", encoding="utf-8") as f: nb = current.read(f, "json") markdown_text: str = "" heading_text: str = "" code_text: str = "" for cell in nb.worksheets[0].cells: if cell.cell_type == "markdown": markdown_text += cell["source"].replace("#", "").lstrip() elif cell.cell_type == "heading": heading_text += cell["source"].replace("#", "").lstrip() elif cell.cell_type == "code": code_text += cell["input"].replace("#", "").lstrip() if returnType == "markdown": return markdown_text elif returnType == "heading": return heading_text elif returnType == "code": return code_text else: return Exception
def process_notebook_file(fname, action='clean', output_fname=None): print("Performing '{}' on: {}".format(action, fname)) orig_wd = os.getcwd() with io.open(fname, 'r') as f: nb = current.read(f, 'json') if action == 'check': os.chdir(os.path.dirname(fname)) run_notebook(nb) remove_outputs(nb) remove_signature(nb) elif action == 'render': os.chdir(os.path.dirname(fname)) run_notebook(nb) else: # Clean by default remove_outputs(nb) remove_signature(nb) os.chdir(orig_wd) if output_fname is None: output_fname = fname with io.open(output_fname, 'w') as f: nb = current.write(nb, f, 'json')
import io import os from nbformat import current total_markdown = 0 total_heading = 0 total_code = 0 total_code_lines = 0 for root, dirs, files in os.walk("."): for file in files: if file.endswith(".ipynb") and not file.endswith("checkpoint.ipynb"): #print(os.path.join(root, file)) with io.open(os.path.join(root, file), 'r', encoding='utf-8') as f: nb = current.read(f, 'json') word_count_markdown = 0 word_count_heading = 0 word_count_code = 0 code_lines_count = 0 for cell in nb.worksheets[0].cells: if cell.cell_type == "markdown": word_count_markdown += len(cell['source'].replace( '#', '').lstrip().split(' ')) elif cell.cell_type == "heading": word_count_heading += len(cell['source'].replace( '#', '').lstrip().split(' ')) elif cell.cell_type == "code": word_count_code += len(cell['input'].replace( '#', '').lstrip().split(' ')) code_lines_count += (len(cell['input'].replace(
""" Usage: python remove_output.py notebook.ipynb [ > without_output.ipynb ] Modified from remove_output by Minrk """ import sys import io import os from nbformat.current import read, write def remove_outputs(nb): """remove the outputs from a notebook""" for ws in nb.worksheets: for cell in ws.cells: if cell.cell_type == 'code': cell.outputs = [] if __name__ == '__main__': fname = sys.argv[1] with io.open(fname, 'r') as f: nb = read(f, 'json') remove_outputs(nb) base, ext = os.path.splitext(fname) new_ipynb = "%s_removed%s" % (base, ext) with io.open(new_ipynb, 'w', encoding='utf8') as f: write(nb, f, version=4) print "wrote %s" % new_ipynb
#!/usr/bin/python3 import sys from nbformat.current import read from IPython.utils.text import strip_ansi fname = sys.argv[1] with open(fname, encoding='utf-8') as f: nb = read(f, 'ipynb') banners = { 'heading': 'Heading %d ------------------', 'markdown': 'Markdown cell ---------------', 'code': 'Code cell -------------------', 'raw': 'Raw cell --------------------', 'output': 'Output ----------------------', } for cell in nb.worksheets[0].cells: if cell.cell_type == 'heading': print(banners['heading'] % cell.level) else: print(banners[cell.cell_type]) if cell.cell_type == 'code': source = cell.input else: source = cell.source print(source) if not source.endswith('\n'): print()
#!/usr/bin/env python def strip_output(nb): for ws in nb.worksheets: for cell in ws.cells: if hasattr(cell, "outputs"): cell.outputs = [] if hasattr(cell, "prompt_number"): del cell["prompt_number"] if __name__ == "__main__": from sys import stdin, stdout from nbformat.current import read, write nb = read(stdin, "ipynb") strip_output(nb) write(nb, stdout, "ipynb") stdout.write("\n")
return r nb_filenames = ["Index.ipynb", "Index-labs1-6.ipynb"] nb_filenames.extend(parse('./LABS')) nb_filenames.extend(parse('.')) is_error = False error_list = [] for nb_filename in nb_filenames: try: print('*' * 80) print(nb_filename) notebook = read(open(nb_filename), 'json') r = NotebookRunner(notebook) r.run_notebook() r.shutdown_kernel() except Exception as e: is_error = True # Get current system exception ex_type, ex_value, ex_traceback = sys.exc_info() error_list.append((nb_filename, e, ex_type, ex_value, ex_traceback)) print('! error' + str(e)) finally: print('*' * 80) # display the summary of all the exceptions