@@ -66,25 +66,30 @@ def _git_pull_latest(repo: git.repo.Repo):
66
66
repo .git .pull ("origin" , "master" )
67
67
68
68
69
- def _python_template (report_path : AnyStr , py_template_dir : AnyStr ) -> AnyStr :
70
- file_name = "{}.py" .format (report_path )
71
- return os .path .join (py_template_dir , file_name )
69
+ def _template (report_path : str , py_template_dir : AnyStr ) -> AnyStr :
70
+ py_path = os .path .join (py_template_dir , "{}.py" .format (report_path ))
71
+ ipynb_path = os .path .join (py_template_dir , "{}.ipynb" .format (report_path ))
72
+
73
+ if os .path .isfile (py_path ):
74
+ return py_path
75
+
76
+ return ipynb_path
72
77
73
78
74
79
def _ipynb_output_path (template_base_dir : AnyStr , report_path : AnyStr , git_hex : AnyStr ) -> AnyStr :
75
80
file_name = _output_ipynb_name (report_path )
76
81
return os .path .join (template_base_dir , git_hex , file_name )
77
82
78
83
79
- def _get_python_template_path (report_path : str , warn_on_local : bool , py_template_dir ) -> str :
84
+ def _get_template_path (report_path : str , warn_on_local : bool , py_template_dir : AnyStr ) -> str :
80
85
if py_template_dir :
81
- return _python_template (report_path , py_template_dir )
86
+ return _template (report_path , py_template_dir )
82
87
else :
83
88
if warn_on_local :
84
89
logger .warning (
85
90
"Loading from notebooker default templates. This is only expected if you are running locally."
86
91
)
87
- return pkg_resources .resource_filename (__name__ , "../notebook_templates_example/{}.py" . format ( report_path ))
92
+ return _template ( report_path , pkg_resources .resource_filename (__name__ , "../notebook_templates_example" ))
88
93
89
94
90
95
def _get_output_path_hex (notebooker_disable_git , py_template_dir ) -> str :
@@ -126,22 +131,25 @@ def generate_ipynb_from_py(
126
131
Pulls the latest version of the notebook templates from git, and regenerates templates if there is a new HEAD
127
132
OR: finds the local template from the template repository using a relative path
128
133
129
- In both cases, this method converts the .py file into an .ipynb file which can be executed by papermill.
134
+ Both .ipynb and .py report templates are handled, where .py templates are converted to .ipynb, which can
135
+ be executed by papermill
130
136
131
137
:param template_base_dir: The directory in which converted notebook templates reside.
132
138
:param report_name: The name of the report which we are running.
133
139
:param notebooker_disable_git: Whether or not to pull the latest version from git, if a change is available.
134
- :param py_template_dir: The directory which contains raw python templates. This should be a subdir in a git repo.
140
+ :param py_template_dir: The directory which contains raw py/ipynb templates. This should be a subdir in a git repo.
135
141
:param warn_on_local: Whether to warn when we are searching for notebooks in the notebooker repo itself.
136
142
137
143
:return: The filepath of the .ipynb which we have just converted.
138
144
"""
139
145
report_path = convert_report_name_into_path (report_name )
140
- python_template_path = _get_python_template_path (report_path , warn_on_local , py_template_dir )
146
+ template_path = _get_template_path (report_path , warn_on_local , py_template_dir )
141
147
output_template_path = _ipynb_output_path (
142
148
template_base_dir , report_path , _get_output_path_hex (notebooker_disable_git , py_template_dir )
143
149
)
144
150
151
+ mkdir_p (os .path .dirname (output_template_path ))
152
+
145
153
try :
146
154
with open (output_template_path , "r" ) as f :
147
155
if f .read ():
@@ -151,14 +159,14 @@ def generate_ipynb_from_py(
151
159
pass
152
160
153
161
# "touch" the output file
154
- print ("Creating ipynb at: %s" , output_template_path )
155
- mkdir_p (os .path .dirname (output_template_path ))
156
- with open (output_template_path , "w" ) as f :
162
+ print ("Writing ipynb to: %s" , output_template_path )
163
+ with open (output_template_path , "w" ):
157
164
os .utime (output_template_path , None )
158
165
159
- jupytext_nb = jupytext .read (python_template_path )
166
+ jupytext_nb = jupytext .read (template_path )
160
167
jupytext_nb ["metadata" ]["kernelspec" ] = kernel_spec () # Override the kernel spec since we want to run it..
161
168
jupytext .write (jupytext_nb , output_template_path )
169
+
162
170
return output_template_path
163
171
164
172
0 commit comments