def addCode(self, input, collapsed=False, language=u("python"), **kwargs): self.__addCell(cell_type=u("code"), collapsed=collapsed, input=input, language=text_type(language), outputs=[], **kwargs)
def addCode(self,input,collapsed=False,language=u("python"),**kwargs): self.__addCell(cell_type=u("code"), collapsed=collapsed, input=input, language=text_type(language), outputs=[], **kwargs)
def __addCell(self, **kwargs): data = Cell(**kwargs) for ct in ["input", "source"]: if ct in data: if isinstance(data[ct], string_types): raw = [text_type(l) for l in data[ct].split("\n")] data[ct] = [l + "\n" for l in raw[:-1]] + raw[-1:] self._cells().append(data)
def __addCell(self,**kwargs): data=Cell(**kwargs) for ct in ["input","source"]: if ct in data: if isinstance(data[ct],string_types): raw=[text_type(l) for l in data[ct].split("\n")] data[ct]=[l+"\n" for l in raw[:-1]]+raw[-1:] self._cells().append(data)
def __init__(self,input=None, nbformat=3, nbformat_minor=0, name=None): """:param input: If this is a string then it is interpreted as a filename. Otherwise as a filehandle. If unset then an empty notebook is contructed :param name: name of the notebook. Only used if a new notebook is created """ self.__content={} if input==None: if name==None: error("Specify at least a name") self.__content={ u("metadata") : { u("name"):text_type(name), u("pyFoam") : { u("createdBy") : "pyFoam", u("createdTime") : asctime() } }, u("nbformat") : nbformat, u("nbformat_minor") : nbformat_minor, u("worksheets") : [ { u("cells"):[] } ] } else: if isinstance(input,string_types): fh=open(input) else: fh=input self.__content=json.load(fh) if ("metadata" not in self.__content or "name" not in self.__content["metadata"] or "nbformat" not in self.__content or "worksheets" not in self.__content): error(str(input),"Notebook does not have the expected format") if len(self.__content["worksheets"])>1: warning(str(input),"has more than one worksheet. Only using the first") elif len(self.__content["worksheets"])==0: error(str(input),"has no worksheets") if "cells" not in self.__content["worksheets"][0]: error(str(input),"has no cells") self.reset([Cell(**c) for c in self]) if u("pyFoam") not in self.__content[u("metadata")]: self.__content[u("metadata")][u("pyFoam")]={ u("createdBy") : "other", u("createdTime") : "unknown" }
def __init__(self, input=None, nbformat=3, nbformat_minor=0, name=None): """@param input: If this is a string then it is interpreted as a filename. Otherwise as a filehandle. If unset then an empty notebook is contructed @param name: name of the notebook. Only used if a new notebook is created """ self.__content = {} if input == None: if name == None: error("Specify at least a name") self.__content = { u("metadata"): { u("name"): text_type(name), u("pyFoam"): { u("createdBy"): "pyFoam", u("createdTime"): asctime() } }, u("nbformat"): nbformat, u("nbformat_minor"): nbformat_minor, u("worksheets"): [{ u("cells"): [] }] } else: if isinstance(input, string_types): fh = open(input) else: fh = input self.__content = json.load(fh) if ("metadata" not in self.__content or "name" not in self.__content["metadata"] or "nbformat" not in self.__content or "worksheets" not in self.__content): error(str(input), "Notebook does not have the expected format") if len(self.__content["worksheets"]) > 1: warning(str(input), "has more than one worksheet. Only using the first") elif len(self.__content["worksheets"]) == 0: error(str(input), "has no worksheets") if "cells" not in self.__content["worksheets"][0]: error(str(input), "has no cells") self.reset([Cell(**c) for c in self]) if u("pyFoam") not in self.__content[u("metadata")]: self.__content[u("metadata")][u("pyFoam")] = { u("createdBy"): "other", u("createdTime"): "unknown" }