You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Issue AttributeError: 'list' object has no attribute 'split' nbconvert/filters/strings.py", line 201, in comment_lines return prefix + ("\n" + prefix).join(text.split("\n"))
#361
Open
ShlomoStept opened this issue
Apr 19, 2023
· 0 comments
An error occurs when processing IPython notebook files with 'nbformat': 4 and 'nbformat_minor': 2 (and possibly other versions). The traceback is as follows:
File : "../lib/python3.9/site-packages/nbconvert/exporters/templateexporter.py", line 421, in from_notebook_node
output = self.template.render(nb=nb_copy, resources=resources)
File ".../jinja2/environment.py", line 1301, in render
self.environment.handle_exception()
File ".../jinja2/environment.py", line 936, in handle_exception
raise rewrite_traceback_stack(source=source)
File ".../jupyter/nbconvert/templates/python/index.py.j2", line 1, in top-level template code
{%- extends 'null.j2' -%}
File ".../share/jupyter/nbconvert/templates/base/null.j2", line 26, in top-level template code
{%- block body -%}
File ".../share/jupyter/nbconvert/templates/base/null.j2", line 29, in block 'body'
{%- block body_loop -%}
File ".../share/jupyter/nbconvert/templates/base/null.j2", line 31, in block 'body_loop'
{%- block any_cell scoped -%}
File "...share/jupyter/nbconvert/templates/base/null.j2", line 87, in block 'any_cell'
{%- block markdowncell scoped-%} {%- endblock markdowncell -%}
File ".../share/jupyter/nbconvert/templates/python/index.py.j2", line 19, in block 'markdowncell'
{{ cell.source | comment_lines }}
File ".../lib/python3.9/site-packages/nbconvert/filters/strings.py", line 201, in comment_lines
return prefix + ("\n" + prefix).join(text.split("\n"))
AttributeError: 'list' object has no attribute 'split'
NOTE: The issue arises because cell.source is a list of strings instead of a single string. As a result, the text.split("\n") operation in the comment_lines function (line 201 in nbconvert/filters/strings.py) fails.
One potential solution to this problem could be to add a function before the output = self.template.render(nb=nb_copy, resources=resources) line in the from_notebook_node method (line 421). This function would take nb_copy as an input and modify it as follows:
for cell in nb_copy.cells:
if isinstance(cell.source, list):
cell.source = ''.join(cell.source)
Alternatively, a try-except block could be added that only runs this code if an exception is raised.
just for reference the function i used that triggered this error is
# Step 0 - Convert the file from bytes to string
file_as_string = file_as_string.decode('utf-8')
# Step 1 - Get the version of the ipynb file, (a) convert it to JSON format, (b) grab the version
ipynb_as_json = json.loads(file_as_string)
# Get the version of the notebook format
ipynb_version = ipynb_as_json["nbformat"]
# Normalize the notebook
n_changes, new_notebook = nbformat.validator.normalize(ipynb_as_json, version=ipynb_version)
# Step 2 - Load the notebook from a string
note_book = nbformat.from_dict(new_notebook)
# Step 3 - Convert the notebook to a Python script
exporter = PythonExporter()
python_version, _ = exporter.from_notebook_node(note_book)
...
The text was updated successfully, but these errors were encountered:
An error occurs when processing IPython notebook files with 'nbformat': 4 and 'nbformat_minor': 2 (and possibly other versions). The traceback is as follows:
Traceback (most recent call last):
File : "../lib/python3.9/site-packages/nbconvert/exporters/templateexporter.py", line 421, in from_notebook_node
output = self.template.render(nb=nb_copy, resources=resources)
File ".../jinja2/environment.py", line 1301, in render
File ".../jinja2/environment.py", line 936, in handle_exception
File ".../jupyter/nbconvert/templates/python/index.py.j2", line 1, in top-level template code
File ".../share/jupyter/nbconvert/templates/base/null.j2", line 26, in top-level template code
File ".../share/jupyter/nbconvert/templates/base/null.j2", line 29, in block 'body'
File ".../share/jupyter/nbconvert/templates/base/null.j2", line 31, in block 'body_loop'
File "...share/jupyter/nbconvert/templates/base/null.j2", line 87, in block 'any_cell'
File ".../share/jupyter/nbconvert/templates/python/index.py.j2", line 19, in block 'markdowncell'
File ".../lib/python3.9/site-packages/nbconvert/filters/strings.py", line 201, in comment_lines
AttributeError: 'list' object has no attribute 'split'
NOTE: The issue arises because cell.source is a list of strings instead of a single string. As a result, the text.split("\n") operation in the comment_lines function (line 201 in nbconvert/filters/strings.py) fails.
One potential solution to this problem could be to add a function before the output = self.template.render(nb=nb_copy, resources=resources) line in the from_notebook_node method (line 421). This function would take nb_copy as an input and modify it as follows:
Alternatively, a try-except block could be added that only runs this code if an exception is raised.
just for reference the function i used that triggered this error is
The text was updated successfully, but these errors were encountered: