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

Single line markdown titles not recognized in notebooks when running through PyCharm #285

Closed
ap-- opened this issue Oct 8, 2024 · 1 comment

Comments

@ap--
Copy link

ap-- commented Oct 8, 2024

When running jupyter through PyCharm, adding a single line markdown cell with a title produces the following code in the ipynb file:

{
 "cells": [
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# My custom title",
   "id": "dfac67e992ee5bb6"
  }
 ]
}

nbproject is then unable to detect the notebook title, which consequently makes ln.track fail.

Culprit is the following code:

# grab source
text = cell["source"][0]
# loop through lines
for line in text.split("\n"):
# if finding a level-1 heading, consider it a title
if line.startswith("# "):
title = line.lstrip("#").strip(" .").strip("\n")
return title

Which assumes that "source" is a list of lines of text. (PyCharm correctly produces a list of strings for multiline markdown cells)

Replacing text = cell["source"][0] with:

   source = cell["source"]
   if isinstance(source, str):
       source = [source]
   text = source[0]

should restore correct behavior. Although to me the entire code block looks like initially it was meant to loop through all lines in the source. Maybe something like the following is more appropriate:

   source = cell["source"]
   if isinstance(source, str):
       source = [source]
   for text in source:
       for line in text.splitlines():
           if line.lstrip().startswith("# "):
                ...
@Koncopd
Copy link
Member

Koncopd commented Oct 22, 2024

This was fixed by #286

@Koncopd Koncopd closed this as completed Oct 22, 2024
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

2 participants