Coder Social home page Coder Social logo

textual-plotext's Introduction

textual-plotext

PlotextPlot in action

A Textual widget wrapper for the Plotext plotting library.

Introduction

The library makes one widget available: PlotextPlot. This widget provides a plt property, which should be used where you would normally use plt as seen in the Plotext documentation.

Let's take the first example from the Plotext README:

import plotext as plt
y = plt.sin() # sinusoidal test signal
plt.scatter(y)
plt.title("Scatter Plot") # to apply a title
plt.show() # to finally plot

The Textual equivalent of this (including everything needed to make this a fully-working Textual application) is:

from textual.app import App, ComposeResult

from textual_plotext import PlotextPlot

class ScatterApp(App[None]):

    def compose(self) -> ComposeResult:
        yield PlotextPlot()

    def on_mount(self) -> None:
        plt = self.query_one(PlotextPlot).plt
        y = plt.sin() # sinusoidal test signal
        plt.scatter(y)
        plt.title("Scatter Plot") # to apply a title

if __name__ == "__main__":
    ScatterApp().run()

The main differences to note are:

  • We're not directly importing plotext.
  • We use PlotextPlot.plt rather than plt.
  • We don't call Plotext's show method, PlotextPlot takes care of this.

Installation

The library can be installed from PyPi:

$ pip install textual-plotext

Once installed you can quickly test the library by running the demo:

$ python -m textual_plotext

The demo app includes many of the examples shown in the Plotext README.

Longer example

For a longer example of how to use the PlotextPlot widget, take a look at examples/textual_towers_weather.py. As a bonus it also shows an example of using Textual's worker API to create a threaded worker to pull the data from the backend.

What is supported?

The following utility functions are provided (via PlotextPlot.plt):

  • plt.sin
  • plt.square
  • plt.colorize
  • plt.uncolorize

What isn't supported?

Some functions are not supported at all; mainly those that would not make sense inside a Textual application. These include:

  • plt.interactive
  • plt.script_folder
  • plt.parent_folder
  • plt.join_paths
  • plt.save_text
  • plt.read_data
  • plt.write_data
  • plt.download
  • plt.delete_file
  • plt.test
  • plt.time
  • plt.test_data_url
  • plt.test_bar_data_url
  • plt.test_image_url
  • plt.test_gif_url
  • plt.test_video_url
  • plt.test_youtube_url

The following properties and sub-modules also aren't exposed because they too are designed for REPL-based interactive use and don't lend themselves to being used in a Textual application:

  • plt.doc
  • plt.markers
  • plt.colors
  • plt.styles
  • plt.themes

Also, currently, there is no support for GIF plots or playing videos; one or both could follow at some point in the future.

What functions are no-ops?

Some functions are supported as calls but are redefined to be non-operations; these are the sorts of code that wouldn't generally have a negative side-effect but which don't make sense for a Textual application. These include:

  • plt.clear_terminal
  • plt.show
  • plt.save_fig

Themes

Plotext has a system of themes. The themes provided by Plotext are supported by PlotextPlot. However, some of those themes use "ANSI colors" and so may end up looking different depending on which terminal you use, and what terminal theme is in use (if your terminal supports themes); the issue here is that your plots may look very different in different environments.

To help with this textual-plotext provides an additional set of copies of those themes, all prefixed with textual-, that use full RGB coloring to ensure that, when using a given Plotext theme, it will look the same everywhere.

In addition to the Plotext themes, textual-plotext also adds two themes called:

  • textual-design-dark
  • textual-design-light

These are two similar themes designed to look good in dark mode and light mode respectively. Out of the box, PlotextPlot will use these and will switch between them when your Textual application switches between dark and light mode. If you wish to turn off this behaviour, simply set the auto_theme property of your plot to False.

Known issues

At the moment, due to what appears to be a bug in Plotext when it comes to repeated calls to show or build1 for a plot with x or y scales set to "log", it isn't possible to easily build such a plot. In other words, even in the REPL with Plotext itself, a session such as this:

>>> import plotext as plt
>>> plt.xscale("log")
>>> plt.plot(plt.sin(periods=2, length=10**4))
>>> plt.show()
<plot is drawn in the terminal here>
>>> plt.show()

results in a ValueError: math domain error.

There is a workaround for this, which will work for Plotext use in general and would also work nicely in a Textual app. After the first show or build, set the problematic scale back to "linear". So the REPL session should above would become:

>>> import plotext as plt
>>> plt.xscale("log")
>>> plt.plot(plt.sin(periods=2, length=10**4))
>>> plt.show()
<plot is drawn in the terminal here>
>>> plt.xscale("linear")     # Note this here!
>>> plt.show()
<plot is drawn in the terminal here>
>>> plt.show()
<plot is drawn in the terminal here>
etc...

In a Textual app, this would mean adding (assuming the xscale was the problem here) this at the end of the code to create the plot:

_ = self.plt.build()
self.plt.xscale("linear")

Need more help?

If you need help with this library, or with anything relating to Textual, feel free to come join the Textualize devs on Discord or the other places where we provide support.

Footnotes

  1. Repeated calls to build will happen when the PlottextPlot widget needs to render the plot again, on resize for example. โ†ฉ

textual-plotext's People

Contributors

davep avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

textual-plotext's Issues

Investigate more Textual-friendly colouring and theming

Introduction

As indirectly pointed out in Textualize/textual#3532 -- some (all?) of the default themes in Plotext use "ANSI colors", and as such won't always look the same in all terminals. While likely fine and desirable when working on the command line, within a Textual application you probably want consistency across the board.

With this issue I'll investigate what options there are for better colouring while plotting within a Textual application, and also map out which of the themes that come with Plotext work with Textual and which don't look so good.

In doing this I'll also ensure that the best out-of-the-box options are used when it comes to light/dark mode and the auto-theme support.

Plotext themes

Out of the box, Plotext has 16 defined themes. Some seem to be using "ANSI colours", some seem to blend better with a Textual application. Below is a table of the themes, in here I'll try and figure out which ones are "Textual-friendly" and which aren't.

Name Light or Dark Textual-friendly
default Light No
clear Both Yes
pro Both Yes
matrix Dark No
windows Dark No
dark Dark No
retro Dark Yes
elegant Both No
mature Both Yes
dreamland Both No
grandpa Both Yes
salad Both Yes
girly Both No
serious Dark Yes
sahara Both No
scream Both Yes

Note that, initially, the "Textual-friendly" column is talking more about the general colours than every specific colour. I'll do a deeper dive into the individual themes once I've got a good overview of this. This was also initially tested by having two different terminals side-by-side, with one having all the "ANSI colours" configured to be black, to try and tease out which theme had an overall problem and which didn't. If in the "black" terminal a significant element of the plot turned black, that was assumed to be an "ANSI colour".

Also note: light/dark-friendly is a bit of a judgement call; most of the themes are so far removed from "normal" Textual application theming that they equally clash in light and dark mode so will be marked as "Both".

Investigate curious plot sizing issues

I've just noticed this in passing while working on a personal project, and need to dive into this some more; but it seems that the following things can happen with the sizes of plots:

  • Some plots can resize just fine when the app layout changes.
  • Some plots won't size past their initial size when the app layout changes.
  • Even with a refresh(layout=True), things work to a point then don't seem to work right.
  • Even more odd, when all things seem to be working, plots seem to end up with a maximum size and they won't go past that; this smells of a possible issue inside Plotext itself, but that needs to be confirmed.

This is all a bit vague because I'm noting this down before I need to step away from the desk. But the starkest difference can be see if you make demo vs make weather, and mess about with resizing the terminal with either of them.

Need to investigate and attempt to fix.

I also now wonder why this was originally why I'd designed the widget so that it always needed a subclass with the plot method implemented?

Tagging @willmcgugan as requested if there are issues very much worth moving from backlog to TODO (at least for the initial investigation of what might be going on).

Feature Request: Tooltips on Hover Dates/Bars on Textual-Plotext

Really enjoying using textual-plotext! It opens up a whole new dimension for textual applications.

Would love to see textual's Tooltips functionality extended to textual-plotext.

Where mouse hovering over a specific bar / datetime / candlestick / within the PlotextPlot
can triggers a callback to format a tooltip at the cursor location.

== Textual as a Plotly / Dash replacement ?

OSError On Clicking "Special Plots" in Demo (Windows)

I've installed textual-plotext
Ran demo with
python -m textual_plotext in command prompt

When I click "Special Plots" the demo crashes with:
OSError [Errno 22] Invalid Argument
As Attached
the last error is in:
...site-packages\plotext_date.py:56 in time_to_string
โ”‚ return self.datetime_to_string(dt.fromtimestamp(time + self.time0), output_form)

full screenshots of stack trace below
1

Running
plotext 5.2.8
Textual 0.40.0
Python 3.11.3
Windows10 Pro build 19045
error appears in both Command Prompt and Windows PowerShell

2
3

Add metadata to textual-plot

There's no link on the PyPi page to the Github repo. We should add that to pyproject.toml

We should also update the blog entry to refer to the repo rather than the PyPi page.

Probably explains the low star count on the repo.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.