mrzv / saturn

plain-text Python notebooks with checkpointing
Other
6 stars 0 forks source link

ImportError: cannot import name 'render_group' from 'rich.console' #1

Closed MooersLab closed 2 years ago

MooersLab commented 2 years ago

Interesting project!!

I installed rich via pip.

I git cloned the project and ran the following code in a kitty terminal.

./saturn.py run samples/simple.py
Traceback (most recent call last):
  File "/Users/blaine/software/saturn/./saturn.py", line 3, in <module>
    import saturn_notebook.__main__
  File "/Users/blaine/software/saturn/saturn_notebook/__main__.py", line 12, in <module>
    from    .           import cells as c, notebook
  File "/Users/blaine/software/saturn/saturn_notebook/notebook.py", line 8, in <module>
    from    .traceback import Traceback
  File "/Users/blaine/software/saturn/saturn_notebook/traceback.py", line 4, in <module>
    from rich.console   import RenderResult, render_group
ImportError: cannot import name 'render_group' from 'rich.console' (/Users/blaine/Library/Python/3.9/lib/python/site-packages/rich/console.py)  
mrzv commented 2 years ago

Thanks for the report. I think I fixed it. Can you try the latest version?

MooersLab commented 2 years ago

Thank you for the fast reply!

After I created a conda env and installed several dependent packages, the script ran without error. However, the images are not showing up in the kitty terminal. The binary code for the png file is inside the output script file, but the image in not being rendered. Which extension(s) did you add to kitty to get the images to appear?

mrzv commented 2 years ago
  1. Why not pip install . in the saturn folder? That should install all the dependencies.
  2. No extensions for kitty. It should just work. What version of kitty do you have?
  3. Can you run saturn version, so we know what it's using?
MooersLab commented 2 years ago
  1. Thank you for the suggestion. It is done. I can run saturn outside of the repo folder and without using a conda env.
  2. I installed png.py and termpdf for use in kitty. Both worked. The command kitty +kitten icat ~/images/5uuuU9C24.png also works.
  3. saturn version returns
    saturn 1.0.5
    wurlitzer 3.0.2
    rich 12.3.0
    ptpython 3.0.20
    dill 0.3.4
    markdown 3.3.6
    atomicwrites 1.4.0
    pygments 2.12.0
    more_itertools 8.12.0
    matplotlib 3.5.1              

I am using a Mac OS 10.15.7 (Catalina).

I can report that saturn works as you advertised on Ubuntu TLS 20.04. That is, the simple.py file displays an image in a kitty terminal. I had python3.8 installed. I had to install Python3.9 to be able to run saturn.

The option to redirect the notebook to a PDF would be useful for sharing static notebooks with people who lack kitty. Exporting to html and then converting to PDF via pandoc is a route.

However, the --html option in the README.md file is not in the on-line help. The usage of the --html flag needs an example. The position of this flag in a command is unclear.

mrzv commented 2 years ago

What version of kitty are you using? Your macOS is older than mine, but I don't see why that should matter. In general, can you give me any more information about this not working with kitty on mac? I cannot guess at the moment what the problem could be.

Python 3.8 vs 3.9 is my mistake (I added this requirement when fixing this issue), but it's an overkill. I can bump it down in the next version.

I pushed an example of --html to the README. PDF is a nice goal, but it would require quite a bit of work. Going through html is the easiest route right now.

MooersLab commented 2 years ago

I am using kitty 0.25.0.

MooersLab commented 2 years ago

From my home directory in a kitty terminal:

saturn show ./software/saturn/samples/simple.py --html notebook.html
open notebook.html

Looks great! Thank you for the example.

mrzv commented 2 years ago

This is most mysterious. Does it fail to display the images when you do saturn run ... or saturn show ..., or both?

MooersLab commented 2 years ago

Fails to display with both

saturn show ./software/saturn/samples/simple.py
saturn clean ./software/saturn/samples/simple.py
saturn run ./software/saturn/samples/simple.py
mrzv commented 2 years ago

What does echo $TERM say?

MooersLab commented 2 years ago

xterm-24bit

mrzv commented 2 years ago

Ah! Then mystery solved. saturn expects $TERM to be xterm-kitty, when running under Kitty. That's how it determines whether to turn on image output.

Now the question is why doesn't yours say xterm-kitty, and how common is this problem?

Meanwhile, you can try export TERM=xterm-kitty and see if it starts working.

MooersLab commented 2 years ago

Thank you very much!!!

Yes, mystery solved! It worked after I entered export TERM=xterm-kitty.

I had called from iterm the kitty binary in /Applications via an alias:

alias kitty=/Applications/kitty.app/Contents/MacOS/kitty

mrzv commented 2 years ago

The alias shouldn't make a difference. I need to find a better way to detect whether we are running under Kitty. I'll investigate what that is.

MooersLab commented 2 years ago

Some post-closure comments for future readers:

The following pandoc command makes the html to pdf conversion.

pandoc -f html -t pdf -o notebook.pdf notebook.html

The conversion to docx also works.

pandoc -f html -t docx -o notebook.docx notebook.html

The conversion to latex fails to add the figure environment.

pandoc -f html -t latex -o notebook.tex notebook.html

You can convert your script to a beamer latex file to make a slideshow. You will have to add figure environments to import images and a preamble with your preferred configuration, but it may save time when preparing a group meeting presentation.

pandoc -f html -t beamer -o notebookbeamer.tex notebook.html

If the Python script can be converted to GitHub markdown with code blocks, pandoc could convert the md documents in a ipynb (Juptyer notebook) document.

In leiu of the markdown version, the program p2j converts a python script to a Jupyter notebook. The conversion is not perfect, but it works well enough and saves time. The code blocks run. Install via pip install p2j.

Then you can convert the Jupyter notebook to a reveal.js slideshow inside Jupyter.

mrzv commented 2 years ago

Thanks for the debrief. I'm going to look for better ways to detect kitty/graphics protocol, but meanwhile I'll close this issue.