def lines_to_notebook(lines, name=None): """ Convert the lines of an m file into an IPython notebook Parameters ---------- lines : list A list of strings. Each element is a line in the m file Returns ------- notebook : an IPython NotebookNode class instance, containing the information required to create a file """ source = [] md = np.empty(len(lines), dtype=object) new_cell = np.empty(len(lines), dtype=object) for idx, l in enumerate(lines): new_cell[idx], md[idx], this_source = format_line(l) # Transitions between markdown and code and vice-versa merit a new # cell, even if no newline, or "%%" is found. Make sure not to do this # check for the very first line! if idx>1 and not new_cell[idx]: if md[idx] != md[idx-1]: new_cell[idx] = True source.append(this_source) # This defines the breaking points between cells: new_cell_idx = np.hstack([np.where(new_cell)[0], -1]) # Listify the sources: cell_source = [source[new_cell_idx[i]:new_cell_idx[i+1]] for i in range(len(new_cell_idx)-1)] cell_md = [md[new_cell_idx[i]] for i in range(len(new_cell_idx)-1)] cells = [] # Append the notebook with loading matlab magic extension notebook_head = "import pymatbridge as pymat\n" + "ip = get_ipython()\n" \ + "pymat.load_ipython_extension(ip)" cells.append(nbformat.new_code_cell(notebook_head, language='python')) for cell_idx, cell_s in enumerate(cell_source): if cell_md[cell_idx]: cells.append(nbformat.new_text_cell('markdown', cell_s)) else: cell_s.insert(0, '%%matlab\n') cells.append(nbformat.new_code_cell(cell_s, language='matlab')) ws = nbformat.new_worksheet(cells=cells) notebook = nbformat.new_notebook(metadata=nbformat.new_metadata(), worksheets=[ws]) return notebook
def create_file(self, model=None, path='', ext='.ipynb'): """Create a new file or directory and return its model with no content.""" path = path.strip('/') if model is None: model = {} if 'content' not in model and model.get('type', None) != 'directory': if ext == '.ipynb': metadata = current.new_metadata(name=u'') model['content'] = current.new_notebook(metadata=metadata) model['type'] = 'notebook' model['format'] = 'json' else: model['content'] = '' model['type'] = 'file' model['format'] = 'text' if 'name' not in model: if model['type'] == 'directory': untitled = self.untitled_directory elif model['type'] == 'notebook': untitled = self.untitled_notebook elif model['type'] == 'file': untitled = self.untitled_file else: raise HTTPError(400, "Unexpected model type: %r" % model['type']) model['name'] = self.increment_filename(untitled + ext, path) model['path'] = path model = self.save(model, model['name'], model['path']) return model
def new_notebook(self): """Create a new notebook and return its notebook_id.""" name = self.increment_filename('Untitled') metadata = current.new_metadata(name=name) nb = current.new_notebook(metadata=metadata) notebook_id = self.write_notebook_object(nb) return notebook_id
def new_notebook_files(name='default.ipynb'): # make a new file ugh metadata = current.new_metadata(name=name) nb = current.new_notebook(metadata=metadata) content = current.writes(nb, format=u'json') file = github.InputFileContent(content) files = {name: file} return files
def new_notebook(self): """Create a new notebook and return its notebook_id.""" path, name = self.increment_filename('Untitled') notebook_id = self.new_notebook_id(name) metadata = current.new_metadata(name=name) nb = current.new_notebook(metadata=metadata) with open(path,'w') as f: current.write(nb, f, u'json') return notebook_id
def new_notebook(self): """Create a new notebook and return its notebook_id.""" path, name = self.increment_filename('Untitled') notebook_id = self.new_notebook_id(name) metadata = current.new_metadata(name=name) nb = current.new_notebook(metadata=metadata) with open(path, 'w') as f: current.write(nb, f, u'json') return notebook_id
def new_notebook(self): """Create a new notebook and return its notebook_id.""" n = Notebook() n.id = str(uuid.uuid4()) n.name = 'New Notebook' metadata = current.new_metadata(name=n.name) nb = current.new_notebook(metadata=metadata) data = current.writes(nb, u'json') n.content = data n.save(force_insert=True) return n.id
def create_notebook_model(self, model=None, path=''): """Create a new notebook and return its model with no content.""" path = path.strip('/') if model is None: model = {} if 'content' not in model: metadata = current.new_metadata(name=u'') model['content'] = current.new_notebook(metadata=metadata) if 'name' not in model: model['name'] = self.increment_filename('Untitled', path) model['path'] = path model = self.save_notebook_model(model, model['name'], model['path']) return model
def new_notebook(self): """Create a new notebook and returns its notebook_id.""" i = 0 while True: name = u'Untitled%i' % i path = self.get_path_by_name(name) if not os.path.isfile(path): break else: i = i + 1 notebook_id = self.new_notebook_id(name) metadata = current.new_metadata(name=name) nb = current.new_notebook(metadata=metadata) with open(path, 'w') as f: current.write(nb, f, u'json') return notebook_id
def new_notebook(self): """Create a new notebook and returns its notebook_id.""" i = 0 while True: name = u"Untitled%i" % i path = self.get_path_by_name(name) if not os.path.isfile(path): break else: i = i + 1 notebook_id = self.new_notebook_id(name) metadata = current.new_metadata(name=name) nb = current.new_notebook(metadata=metadata) with open(path, "w") as f: current.write(nb, f, u"json") return notebook_id
def new_notebook_object(self, name): """ """ metadata = current.new_metadata(name=name) nb = current.new_notebook(metadata=metadata) return nb
def new_notebook(self): model = {} metadata = current.new_metadata(name=u'') model['content'] = current.new_notebook(metadata=metadata) return model