jupyter
jupyter copied to clipboard
Use of """ in docstrings does not work in org src block
I wanted to share this issue I recently found. Not sure this has been discussed already.
Here a simple example of code that generate an error:
#+begin_src jupyter-python
class Example():
"""docstring"""
#+end_src
#+RESULTS:
:RESULTS:
# [goto error]
: Cell In[155], line 5
: get_ipython().run_cell(r"""class Example():
: ^
: SyntaxError: invalid syntax. Perhaps you forgot a comma?
:END:
The way I found to avoid this is to replace """ with '''. I believe the use of """ is recommended or at least very common. Is there a way to fix this? Or there's something broken in my org config.
Thanks!
testing a little more I found that a possible cause of this is setting the option :dir (which I had set in the #+PROPERTY: in the example above).
#+begin_src jupyter-python :dir ./
class example():
"""docstring"""
print("test")
#+end_src
#+RESULTS:
:RESULTS:
# [goto error]
: Cell In[22], line 5
: get_ipython().run_cell(r"""class example():
: ^
: SyntaxError: invalid syntax. Perhaps you forgot a comma?
:END:
without :dir, it works fine
#+begin_src jupyter-python
class example():
"""docstring"""
print("ciao")
#+end_src
#+RESULTS:
: ciao
Also, this happens in similar way:
#+begin_src jupyter-python :dir ./
var = "test"
#+end_src
#+RESULTS:
:RESULTS:
# [goto error]
: Cell In[32], line 5
: get_ipython().run_cell(r"""var = "test"""")
: ^
: SyntaxError: unterminated string literal (detected at line 5)
:END:
here, removing the :dir flag works, but also adding a newline or a space after the var = "test" fixes the issue even keeping the :dir flag
#+begin_src jupyter-python :dir ./
var = "test"
#+end_src
#+RESULTS:
Thanks for reporting. I believe I've fixed it.
Thanks a lot, I'll give it a try!