def get(self, cid): """Retrieve (and build) notebook for a single contribution [internal]. --- operationId: get_entry parameters: - name: cid in: path type: string pattern: '^[a-f0-9]{24}$' required: true description: contribution ID (ObjectId) responses: 200: description: single notebook schema: $ref: '#/definitions/NotebooksSchema' """ try: nb = Notebooks.objects.get(id=cid) nb.restore() except DoesNotExist: cells = [ nbf.new_code_cell( "# provide apikey to `load_client` in order to connect to api.mpcontribs.org\n" "# or use bravado (see https://mpcontribs.org/api)\n" "from mpcontribs.client import load_client\n" "client = load_client()" ), nbf.new_code_cell( "from mpcontribs.io.archieml.mpfile import MPFile\n" f"result = client.contributions.get_entry(cid='{cid}').response().result\n" "mpfile = MPFile.from_contribution(result)" ) ] for typ in ['h', 't', 'g', 's']: cells.append(nbf.new_code_cell(f"mpfile.{typ}data")) nb = nbf.new_notebook() nb['cells'] = cells exprep.preprocess(nb, {}) nb = Notebooks(**nb) nb.id = cid # to link to the according contribution nb.save() # calls Notebooks.clean() del nb.id return nb
def get(self, cid): """Retrieve (and build) notebook for a single contribution [internal]. --- operationId: get_entry parameters: - name: cid in: path type: string pattern: '^[a-f0-9]{24}$' required: true description: contribution ID (ObjectId) responses: 200: description: single notebook schema: $ref: '#/definitions/NotebooksSchema' """ try: nb = Notebooks.objects.get(id=cid) nb.restore() except DoesNotExist: cells = [ nbf.new_code_cell( "# provide apikey to `load_client` in order to connect to api.mpcontribs.org\n" "# or use bravado (see https://mpcontribs.org/api)\n" "from mpcontribs.client import load_client\n" "client = load_client()"), nbf.new_code_cell( "from mpcontribs.io.archieml.mpfile import MPFile\n" f"result = client.contributions.get_entry(cid='{cid}').response().result\n" "mpfile = MPFile.from_contribution(result)") ] for typ in ['h', 't', 'g', 's']: cells.append(nbf.new_code_cell(f"mpfile.{typ}data")) nb = nbf.new_notebook() nb['cells'] = cells exprep.preprocess(nb, {}) nb = Notebooks(**nb) nb.id = cid # to link to the according contribution nb.save() # calls Notebooks.clean() del nb.id return nb
def get(self, cid): """Retrieve (and build) notebook for a single contribution [internal]. --- operationId: get_entry parameters: - name: cid in: path type: string pattern: '^[a-f0-9]{24}$' required: true description: contribution ID (ObjectId) responses: 200: description: single notebook schema: $ref: '#/definitions/NotebooksSchema' """ try: nb = Notebooks.objects.get(id=cid) nb.restore() except DoesNotExist: contrib = Contributions.objects.no_dereference().get(id=cid) cells = [ nbf.new_code_cell( "client = load_client() # provide apikey as argument to use api.mpcontribs.org\n" f"contrib = client.contributions.get_entry(cid='{cid}').response().result" ), nbf.new_markdown_cell("## Provenance Info"), nbf.new_code_cell( "mask = ['title', 'authors', 'description', 'urls', 'other', 'project']\n" "prov = client.projects.get_entry(project=contrib['project'], mask=mask).response().result\n" "RecursiveDict(prov)"), nbf.new_markdown_cell( f"## Hierarchical Data for {contrib['identifier']}"), nbf.new_code_cell("HierarchicalData(contrib['content'])") ] tables = contrib.content['tables'] if tables: cells.append( nbf.new_markdown_cell( f"## Tabular Data for {contrib['identifier']}")) for ref in tables: cells.append( nbf.new_code_cell( f"table = client.tables.get_entry(tid='{ref.id}').response().result # Pandas DataFrame format\n" "Table.from_dict(table)")) cells.append(nbf.new_code_cell("Plot.from_dict(table)")) structures = contrib.content['structures'] if structures: cells.append( nbf.new_markdown_cell( f"## Pymatgen Structures for {contrib['identifier']}")) for ref in structures: cells.append( nbf.new_code_cell( f"Structure.from_dict(client.structures.get_entry(sid='{ref.id}').response().result)" )) kernel = client.start_kernel() for cell in cells: if cell.cell_type == 'code': cell.outputs = kernel.execute(cell.source) client.shutdown_kernel(kernel) nb = deepcopy(seed_nb) nb.cells += cells nb = Notebooks(**nb) nb.id = cid # to link to the according contribution nb.save() # calls Notebooks.clean() del nb.id return nb