olavolav / uniplot

Lightweight plotting to the terminal. 4x resolution via Unicode.
MIT License
370 stars 16 forks source link

Get a plot as a string #10

Closed JoshKarpel closed 3 years ago

JoshKarpel commented 3 years ago

I'm working on a tool that I would love to be able to integrate uniplot into, but I'm running into a problem: I need to capture the output as a string so that I can display it elsewhere. Right now, uniplot.plot prints directly to stdout: https://github.com/olavolav/uniplot/blob/master/uniplot/uniplot.py#L74

I'm not sure how to square this with the interactive features implemented in that function. Perhaps a helper function could be extracted that returns the plot as a string? Then I could call that function from my tool to get the string that uniplot.plot would have displayed.

Happy to do the work on this if you can provide some guidance on how you'd like it implemented!

olavolav commented 3 years ago

Hi @JoshKarpel thanks for the request!

Should be possible fairly easily. So let's say you had a function plot_to_string that has the same signature as plot but without the interactive option, would that help already?

In particular right now uniplot does not guarantee a horizontal dimension, as width is the width option plus how much space the vertical axis labels take. Depending on how you want to integrate the output, would that be an issue for you?

JoshKarpel commented 3 years ago

So let's say you had a function plot_to_string that has the same signature as plot but without the interactive option, would that help already?

Definitely!

In particular right now uniplot does not guarantee a horizontal dimension, as width is the width option plus how much space the vertical axis labels take. Depending on how you want to integrate the output, would that be an issue for you?

I don't think it's a deal breaker, but it might make things easier. I could certainly do without it for a first pass!

My current thinking is that I'll probably be sticking the plot output inside a https://rich.readthedocs.io/en/stable/reference/text.html#rich.text.Text , and I can disable wrapping - but that means the plot might get cut off if I don't guess the width right.

olavolav commented 3 years ago

Thanks @JoshKarpel!

I'll have a look over the weekend, but I think it shouldn't be too hard, might actually end up with more readable code when splitting the plot generation and text output.

I also thought about limiting the bounding box of the plot, this should also be possible by computing the vertical axis labels first, and then shrinking the plot window if needed. But I'll leave this for a later date.

olavolav commented 3 years ago

Hi @JoshKarpel I just added such a function in version 0.4.4 🚢

Haven't gotten around to do the "hard cap on line length" part yet, but hope this helps already.

If you can share I'd be curious to see what you end up with on your side!

JoshKarpel commented 3 years ago

Hi @JoshKarpel I just added such a function in version 0.4.4 🚢

Haven't gotten around to do the "hard cap on line length" part yet, but hope this helps already.

If you can share I'd be curious to see what you end up with on your side!

It's working really well so far! https://github.com/JoshKarpel/spiel/pull/33

image

olavolav commented 3 years ago

Very cool! 😄 Looking forward to see how Spiel progresses