rich icon indicating copy to clipboard operation
rich copied to clipboard

Automatic Jupyter environment detection

Open big-o opened this issue 6 years ago • 10 comments

Something similar to tqdm's auto module would be helpful. This would mean I can run the same code in Jupyter or a terminal and get the same rich experience.

Basically, it would just be a small wrapper that does a simple check to whether or not we are in an IPython environment or a regular Python one, and then import the console and print objects from the correct package. Then you could write code like this, knowing it would work wherever it was run:

from rich.auto import print

print("[bold magenta]Hello, world![\]")

Alternatively you could put this functionality into the default imports; I'm not sure why you would ever not want this (other than to avoid the overhead of checking, but that's fairly minimal).

big-o avatar May 05 '20 09:05 big-o

Yeah, I think I could do something along those lines.

willmcgugan avatar May 05 '20 10:05 willmcgugan

Fantastic! This project is amazing by the way, literally the only thing holding me back from using it more at the moment is the Jupyter support. I'm sure that things like this and getting a Jupyter-friendly version of the progress bar would probably really help to expand uptake.

big-o avatar May 05 '20 11:05 big-o

Another big vote for this feature - would love to be able to use this inline in Jupyter.

lukexyz avatar May 08 '20 05:05 lukexyz

For those interested, I have a 'jupyter' branch with improved Jupyter support.

It's completely transparent now. The same imports and API will work in the terminal and notebook. Here is a screenshot:

Screen Shot 2020-05-28 at 17 46 32

willmcgugan avatar May 28 '20 16:05 willmcgugan

This looks awesome! I just had a go and it works great :) I noticed that progress bars still don't work; is this something that will get added later on or would that be a separate piece of work (happy to start up a issue for that if you need).

big-o avatar May 28 '20 17:05 big-o

Looks awesome!!

lukexyz avatar May 28 '20 17:05 lukexyz

Just noticed that the link rendering isn't quite working for me. That could be because I'm in Jupyter Lab rather than notebooks. FYI here's what I get:

Screenshot from 2020-05-28 18-14-28

big-o avatar May 28 '20 17:05 big-o

Glad you like it.

Not sure about the progress bars TBH. If I have to write some Javascript widget, then that's a bit beyond the remit of this library. Hopefully there is a way to re-use the existing progress bars without too much effort.

@big-o Your data is a tuple. Try print(*data)

willmcgugan avatar May 28 '20 17:05 willmcgugan

Exploding the tuple fixed it, thanks :)

big-o avatar May 28 '20 17:05 big-o

Auto-magic Jupyter detection is in v1.3.0 now. Progress bars are a possibility for a future release. If I can figure out a nice way to integrate them with Jupyter.

willmcgugan avatar May 31 '20 16:05 willmcgugan