Skip to content

Unable to export notebook as pdf or slide if a cell source contains latex / string[] in it #167

@ccjoel

Description

@ccjoel

Error / Facts

When trying to export a notebook (have some cells, press export > PDF on Beaker UI), beaker_kernel calls:

beaker-kernel/beaker_kernel/service/handlers.py:375:

            output, resources = exporter.from_notebook_node(
              ... #redacted
            )

We ran into this stacktrace:

Traceback (most recent call last):
  File "/opt/biome-python/lib/python3.12/site-packages/beaker_kernel/service/handlers.py", line 375, in post
    output, resources = exporter.from_notebook_node(
                        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
...

  File "/opt/biome-python/share/jupyter/nbconvert/templates/latex/document_contents.tex.j2", line 68, in block 'markdowncell'
    ((( cell.source | citation2latex | strip_files_prefix | convert_pandoc('markdown+tex_math_double_backslash', 'json',extra_args=[]) | resolve_references | convert_explicitly_relative_paths | convert_pandoc('json','latex'))))

...

File "/opt/biome-python/lib/python3.12/site-packages/nbconvert/filters/citation.py", line 114, in feed
    HTMLParser.feed(self, data)
  File "/usr/lib/python3.12/html/parser.py", line 111, in feed
    self.rawdata = self.rawdata + data
                   ~~~~~~~~~~~~~^~~~~~
TypeError: can only concatenate str (not "list") to str

Analysis

The from_notebook_node's chain of calls was caused by the markdown cell's cell.source of type str[] and/or latex situation.

Internally, the method tried to apply a jinja template filter that expects a string, not a list of strings (citation2latex... which turns markdown-style citations into LaTeX bibliography commands). Internally, it uses python’s HTMLParser.feed(data), where data must be a string. The notebook cell spec does include str/str[] as options, which means parser.feed([str1, str2...]) will throw a TypeError.

The fix, as I've found, is related to issue #149, although in that case it occurs when importing a markdown cell and only displays an empty markdown cell in the UI if the cell's source is a list of strings.

Reproducing

On Beaker UI, port 8888 (which allows port exports than on vite ui-dev server), import this notebook:

minimal-export-issue-notebook.ipynb

Then press the download icon, then PDF or slides option on the menu:

Image

Potential Solutions

We have two options to address these issues:

  1. Normalize once at load, or cell create time (even when created by LLM), or on import
  2. Patch locally before export
  3. Combination of both (1, which includes after import, 2, which includes before export)

What we tried

To fix, we used approach (2.) as outlined above, modifying service/handlers.py in branch bugfix-export-import-cell-source-string-list.

See Diff at https://github.com/jataware/beaker-kernel/compare/bugfix-export-import-cell-source-string-list?expand=1

This helped fix the stacktrace issue (which btw is pretty ugly on the frontend side). However, now that exporting was possible, both PDF and slide export results were not ideal.

PDF Export Result after fix

PDF file creation to around 30 seconds with no UI feedback, and eventually a file was created and downloaded.
Unfortunately, the downloaded file is unrecognizable by my OS with error "File may be damaged...".

Slides Export Result after fix

Upon opening in browser, exporting a particular notebook looked badly styled, and missing many cell contents:

Image

A simpler notebook with only 2 minimal sample cells rendered Ok, however.

More References:

More Background

A. Error exposed on UI while trying to export as PDF before any temporary fixes applied:

Image

B. Server logs when exporting PDF after fix:

[WARNING] Div at line 4 column 1 unclosed at line 9 column 1, closing implicitly.
[WARNING] Div at line 3 column 1 unclosed at line 9 column 1, closing implicitly.
[WARNING] Div at line 4 column 1 unclosed at line 9 column 1, closing implicitly.
[WARNING] Div at line 3 column 1 unclosed at line 9 column 1, closing implicitly.
[I 2025-09-16 10:59:33.074 BeakerServerApp] Support files will be in
[I 2025-09-16 10:59:33.075 BeakerServerApp] Writing 252860 bytes to notebook.tex
[I 2025-09-16 10:59:33.075 BeakerServerApp] Building PDF
[I 2025-09-16 10:59:33.076 BeakerServerApp] Running xelatex 3 times: ['xelatex', 'notebook.tex', '-quiet']

[I 2025-09-16 10:59:39.996 BeakerServerApp] Running bibtex 1 time: ['bibtex', 'notebook']
[W 2025-09-16 10:59:40.099 BeakerServerApp] bibtex had problems, most likely because there were no citations
[I 2025-09-16 10:59:40.099 BeakerServerApp] PDF successfully created

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions