Skip to main content

A Widget implementation of PyFiglet for Textual

Project description

   __            __              __                    
  / /____  _  __/ /___  ______ _/ /                    
 / __/ _ \| |/_/ __/ / / / __ `/ /_____                
/ /_/  __/>  </ /_/ /_/ / /_/ / /_____/                
\__/\___/_/|_|\__/\__,_/\__,_/_/                       
                 _____       __     __                 
    ____  __  __/ __(_)___ _/ /__  / /_                
   / __ \/ / / / /_/ / __ `/ / _ \/ __/                
  / /_/ / /_/ / __/ / /_/ / /  __/ /_                  
 / .___/\__, /_/ /_/\__, /_/\___/\__/                  
/_/    /____/      /____/                              

Base package - includes 10 fonts (71kb):

pip install textual-pyfiglet

Install with extended fonts collection - 519 fonts (1.6mb):

pip install textual-pyfiglet[fonts]

Textual-PyFiglet is an implementation of PyFiglet for Textual.

It provides a FigletWidget which is designed to be easy to use inside of Textual.

Demo GIF

Key features

Textual-PyFiglet is a fork of PyFiglet:

The original PyFiglet has zero dependencies, since it's a full re-write of FIGlet in Python. I've aimed to recreate this light-weight nature as much as possible. Textual-PyFiglet has one dependency, platformdirs

The full git history of PyFiglet is properly preserved.

Extended fonts collection moved to separate package:

If you want the whole collection, simply use:
pip install textual-pyfiglet[fonts]

The included 10 fonts were chosen for being minimalist and normal-looking

PyFiglet wheel: 1.1 MB. --> Textual-PyFiglet wheel: 71 KB.

Most of the size of PyFiglet is just the massive fonts collection, 519 in total. In the base textual-pyfiglet package I've included only 10 of the best minimal fonts. I've also made it easy to download the full collection for those who still want it (use extended fonts install, shown at the top)

Widget easily drops into your Textual app:

The widget is based on Static and is designed to mimick its behavior. That means it can drop-in replace any Static widget, and it should just work without even adding or changing arguments (using default font). Assuming you're accounting for the size of the text somehow.

You can dynamically set the size (ie 1fr, 100%) as you would with any Textual widget. It will respond automatically to any widget resize events, and re-draw the figlet. If the widget is set to the screen size, PyFiglet will wrap to the screen.

Real-time updating:

As you would expect with a good Figlet program, the text can update in real-time as you type, or receive input from whatever else you desire.

It's easy to implement in your own Textual app. See below.

Usage

Demo program, CLI:

Run the demo program with either:
textual-pyfiglet
Or:
python -m textual_pyfiglet

PyFiglet also has its own CLI which has been kept available. (Which has its own built-in demo program.) You can access the PyFiglet CLI with:
python -m textual_pyfiglet.pyfiglet

Try it out to see the options. For instance, try running this code:
python -m textual_pyfiglet.pyfiglet Hey guys, whats up?

How to use:

FigletWidget is designed to be used like a normal Static widget.

You can simply create one with the Textual syntax:

from textual_pyfiglet import FigletWidget

def compose(self):
   yield FigletWidget("Label of Things", id="figlet1")

In this case it will use the default font, Calvin_s. You can also specify a font in the constructor:

yield FigletWidget("Label of Things", id="figlet1" font="small")

Resizing

The FigletWidget will auto-update the rendering area whenever it gets resized.
Internally it uses Textual's on_resize method. So it will work automatically.
Just set the widget to the size you want, and PyFiglet will render what it can in that space.

Change the font / Justification

The widget will update automatically when this is run:

self.query("#figlet1").set_font("small")
Base fonts
calvin_s smblock
chunky smbraille
cybermedium standard
small_slant stick_letters
small tmplr

If the extended fonts pack is not installed, the widget will do a quick check every launch to see if its been downloaded. So you can install it afterwards any time you feel like it.

To set the justification, use this method. Options are 'left', 'right', 'center', 'auto'

self.query("#figlet1").set_justify("left")

Live updating / Passing text

To update the FigletWidget with new text, simply pass it in the update method:

self.query_one("#figlet1").update("New text here")

For instance, if you have a TextArea widget where a user can enter text, you can do this:

@on(TextArea.Changed)
def text_changed(self):
   text = self.query_one("#text_input").text
   self.query_one("#figlet1").update(text)

The FigletWidget will then auto-update with every key-stroke.
Note that you cannot pass in a Rich renderable, like the normal Static widget - the text has to be a normal string for PyFiglet to work.

You can access two lists of installed fonts through this method in the FigletWidget:

figlet1 = self.query_one("#figlet1")
all_fonts = figlet1.get_fonts_list(get_all=True)
base_fonts = figlet1.get_fonts_list(get_all=False)  # only get standard 10

Regular PyFiglet / String

You can still import PyFiglet and use it normally:

from textual_pyfiglet.pyfiglet import Figlet         # class version
from textual_Pyfiglet.pyfiglet import figlet_format  # function version

If you just need a quick way to grab a figlet as a string, the pyfiglet.figlet_format function is often the easiest. There's also two convenience methods in the FigletWidget class:

self.query_one("#figlet1").copy_text_to_clipboard()
fig_string = self.query_one("#figlet1").return_figlet_as_string()

Thanks and Copyright

Both Textual-Pyfiglet and the original PyFiglet are under MIT License. See LICENSE file.

FIGlet fonts have existed for a long time, and many people have contributed over the years.

Thanks to original creators of FIGlet:
https://www.figlet.org

Thanks to the PyFiglet team:
https://github.com/pwaller/pyfiglet

The website of another prominent FIG programmer was extremely helpful:
https://patorjk.com/software/taag/

Thanks to Textual:
https://github.com/Textualize/textual

And finally, thanks to the many hundreds of people that contributed to the fonts collection.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

textual_pyfiglet-0.5.5.tar.gz (64.2 kB view details)

Uploaded Source

Built Distribution

textual_pyfiglet-0.5.5-py3-none-any.whl (71.3 kB view details)

Uploaded Python 3

File details

Details for the file textual_pyfiglet-0.5.5.tar.gz.

File metadata

  • Download URL: textual_pyfiglet-0.5.5.tar.gz
  • Upload date:
  • Size: 64.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.4 CPython/3.10.12 Linux/5.15.167.4-microsoft-standard-WSL2

File hashes

Hashes for textual_pyfiglet-0.5.5.tar.gz
Algorithm Hash digest
SHA256 970a941afc8e54e5319b66516a80b2883c3611a28208ccb505f0c3b8b36bb934
MD5 cb12d2476649c64d403ab2b1e842dd26
BLAKE2b-256 f3067b9aad121af1ea25581deb7dbe77f2244ff25e06674f626bfcabbaf66369

See more details on using hashes here.

File details

Details for the file textual_pyfiglet-0.5.5-py3-none-any.whl.

File metadata

  • Download URL: textual_pyfiglet-0.5.5-py3-none-any.whl
  • Upload date:
  • Size: 71.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.4 CPython/3.10.12 Linux/5.15.167.4-microsoft-standard-WSL2

File hashes

Hashes for textual_pyfiglet-0.5.5-py3-none-any.whl
Algorithm Hash digest
SHA256 5b947c243af08c01345ea3c9a235e08e0669dd2d9eae0fa14cd1043a9fa483bd
MD5 a236ae54cec121fdfbab7a962db859fc
BLAKE2b-256 cd5d8275bf13a1e838e9206b0fea96f34d759f0c3020a2064fc7252eaaabb3fd

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page