44
55from django .template import engines
66
7+ from user_templates_api .templates .jupyter_lab .utils .convert_templates .convert_notebook import (
8+ conversion ,
9+ )
10+
711# from nbformat.v4 import new_code_cell, new_markdown_cell
812# import user_templates_api.templates.jupyter_lab.utils.utils as jl_utils
913
1014
1115class JupyterLabRender :
1216 def render (self , data ):
13- # We should append the cells
14- # TODO: Check for the template format in metadata.json
15- # and use the appropriate method.
16-
1717 metadata = data ["metadata" ]
1818 data ["uuids" ] = data .get ("uuids" , [])
1919
20- if metadata ["template_format" ] == "python" :
21- cells = self .python_generate_template_data (data )
22- elif metadata ["template_format" ] == "json" :
23- cells = self .json_generate_template_data (data )
24- elif metadata ["template_format" ] == "jinja" :
25- cells = self .jinja_generate_template_data (data )
20+ if metadata ["template_format" ] != "jinja" :
21+ return
22+
23+ cells = self .jinja_generate_template_data (data )
2624
2725 nb = {"cells" : cells , "metadata" : {}, "nbformat" : 4 , "nbformat_minor" : 5 }
2826
2927 return json .dumps (nb )
3028
31- def python_generate_template_data (self , data ):
32- return []
33-
34- # TODO: see if this can be used without get_client
35- # def json_generate_template_data(self, data):
36- # django_engine = engines["django"]
37-
38- # uuids = data["uuids"]
39- # group_token = data["group_token"]
40-
41- # util_client = get_client(group_token)
42-
43- # template_json = data.get("template")
44- # if template_json is None:
45- # class_file_path = inspect.getfile(self.__class__)
46- # # Convert the string to a pathlib Path
47- # class_file_path = Path(class_file_path)
48- # # Grab the parent path and append template.json
49- # template_file_path = class_file_path.parent / "template.json"
50- # # Load that filepath since it should be the json template
51- # template_json = json.load(open(template_file_path))
52-
53- # cells = []
54- # for template_item in template_json:
55- # cell_type = template_item["cell_type"]
56- # src = template_item["src"]
57- # if cell_type == "template_cell":
58- # if src == "get_metadata_cells":
59- # cells += jl_utils.get_metadata_cells(uuids, util_client)
60- # elif src == "get_file_cells":
61- # cells += jl_utils.get_file_cells(uuids, util_client)
62- # elif src == "get_anndata_cells":
63- # cells += jl_utils.get_anndata_cells(uuids, util_client)
64- # elif cell_type == "code_cell":
65- # template = django_engine.from_string(src)
66- # # Need to do something w/ the source to make sure that it has its vars replaced
67- # # Have to use append here since the nbformat cell object has an add/iadd function that is really a merge
68- # cells.append(new_code_cell(template.render(data)))
69- # elif cell_type == "markdown_cell":
70- # template = django_engine.from_string(src)
71- # # Need to do something w/ the source to make sure that it has its vars replaced
72- # cells.append(new_markdown_cell(template.render(data)))
73-
74- # return cells
75-
7629 def jinja_generate_template_data (self , data ):
7730 django_engine = engines ["django" ]
7831
@@ -81,10 +34,10 @@ def jinja_generate_template_data(self, data):
8134 # Convert the string to a pathlib Path
8235 class_file_path = Path (class_file_path )
8336 # Grab the parent path and append template.txt
84- template_file_path = class_file_path .parent / "template.txt "
37+ template_file_path = class_file_path .parent / "template.ipynb "
8538 # Load that filepath since it should be the json template
8639 template_file = open (template_file_path )
87- template = django_engine .from_string (template_file .read ())
40+ template = django_engine .from_string (conversion ( template_file .read () ))
8841 rendered_template = template .render (data ).strip ()
8942 rendered_template = json .loads (rendered_template ) if rendered_template else {}
9043
0 commit comments