def put(self, obj, name, attributes=None): """ Store a NotebookNode :param obj: the NotebookNode to store :param name: the name of the notebook """ if not name.endswith('.ipynb'): name += '.ipynb' sbuf = StringIO() bbuf = BytesIO() # nbwrite expects string, fs.put expects bytes nbwrite(obj, sbuf, version=4) sbuf.seek(0) bbuf.write(sbuf.getvalue().encode('utf8')) bbuf.seek(0) # see if we have a file already, if so replace the gridfile meta = self.store.metadata(name) if not meta: filename = uuid4().hex fileid = self._fs.put(bbuf, filename=filename) meta = self.store._make_metadata( name=name, prefix=self.store.prefix, bucket=self.store.bucket, kind=self.kind, attributes=attributes, gridfile=GridFSProxy(grid_id=fileid)) else: meta.gridfile.replace(bbuf) return meta.save()
def create_jupyter_notebook(): notebook_fname = '{{cookiecutter.module_name}}.ipynb' nb = new_notebook() nb['cells'] = notebook_cells with open(notebook_fname, 'w') as f: nbwrite(nb, f)
def put(self, obj, name, attributes=None): """ Store a NotebookNode :param obj: the NotebookNode to store :param name: the name of the notebook """ if not name.endswith('.ipynb'): name += '.ipynb' sbuf = StringIO() bbuf = BytesIO() # nbwrite expects string, fs.put expects bytes nbwrite(obj, sbuf, version=4) sbuf.seek(0) bbuf.write(sbuf.getvalue().encode('utf8')) bbuf.seek(0) # see if we have a file already, if so replace the gridfile meta = self.store.metadata(name) if not meta: filename = uuid4().hex fileid = self._fs.put(bbuf, filename=filename) meta = self.store._make_metadata( name=name, prefix=self.store.prefix, bucket=self.store.bucket, kind=self.kind, attributes=attributes, gridfile=GridFSProxy(grid_id=fileid)) meta = meta.save() else: filename = uuid4().hex meta.gridfile.delete() fileid = self._fs.put(bbuf, filename=filename) meta.gridfile = GridFSProxy(grid_id=fileid) meta = meta.save() # set config nb_config = self.get_notebook_config(name) meta_config = meta.attributes.get('config', {}) if nb_config: meta_config.update(dict(**nb_config)) meta.attributes['config'] = meta_config meta = meta.save() if not name.startswith('results') and ('run-at' in meta_config): meta = self.schedule(name) return meta
def convert_mfile(mfile, outfile=None): """ Convert a Matlab m-file into a Matlab notebook in ipynb format Parameters ---------- mfile : string Full path to a matlab m file to convert outfile : string (optional) Full path to the output ipynb file """ lines = mfile_to_lines(mfile) nb = lines_to_notebook(lines) if outfile is None: outfile = mfile.split('.m')[0] + '.ipynb' with open(outfile, 'w') as fid: nbwrite(nb, fid)