Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Comments

@ShlomoStept
Copy link

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):

  • python_version, _ = exporter.from_notebook_node(note_book)

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)
...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant