Skip to content

Commit cf52551

Browse files
committed
Save temp file next to notebook for WebPDF export
By putting the HTML file in the same location as the notebook, any relative file paths correctly resolve. This change saves us from needing to convert relative paths to absolute. This slightly changes behavior by creating the temp file in the user's working directory. We already make sure we delete this temp file, and by adding the ".webpdf-" prefix to make it hidden, less-technical users should never see this file at all.
1 parent 216550b commit cf52551

1 file changed

Lines changed: 4 additions & 3 deletions

File tree

nbconvert/exporters/webpdf.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ def _template_name_default(self):
7878
""",
7979
).tag(config=True)
8080

81-
def run_playwright(self, html):
81+
def run_playwright(self, html, path=None):
8282
"""Run playwright."""
8383

8484
async def main(temp_file):
@@ -160,7 +160,7 @@ async def main(temp_file):
160160
# before calling Chromium. We also specify delete=False to ensure the
161161
# file is not deleted after closing (the default behavior).
162162
temp_file = tempfile.NamedTemporaryFile( # noqa: SIM115
163-
suffix=".html", delete=False
163+
suffix=".html", prefix=".webpdf-", dir=path, delete=False
164164
)
165165
with temp_file:
166166
temp_file.write(html.encode("utf-8"))
@@ -174,9 +174,10 @@ async def main(temp_file):
174174
def from_notebook_node(self, nb, resources=None, **kw):
175175
"""Convert from a notebook node."""
176176
html, resources = super().from_notebook_node(nb, resources=resources, **kw)
177+
path = resources.get("metadata", {}).get("path")
177178

178179
self.log.info("Building PDF")
179-
pdf_data = self.run_playwright(html)
180+
pdf_data = self.run_playwright(html, path)
180181
self.log.info("PDF successfully created")
181182

182183
# convert output extension to pdf

0 commit comments

Comments
 (0)