Skip to main content

Generate sparklines for numbers using Unicode characters only.

Project description

https://img.shields.io/github/actions/workflow/status/deeplook/sparklines/python-package.yml https://img.shields.io/pypi/pyversions/sparklines.svg https://img.shields.io/pypi/v/sparklines.svg https://static.pepy.tech/badge/sparklines/month https://img.shields.io/pypi/status/sparklines.svg https://img.shields.io/pypi/format/sparklines.svg https://img.shields.io/pypi/l/sparklines.svg

This Python package implements Edward Tufte’s concept of sparklines, but limited to text only e.g. like this: ▃▁▄▁▅█▂▅ (this I likely not displayed correctly in every browser). You can find more information about sparklines on Wikipedia. This code was mainly developed for running simple plausibility tests in sensor networks as shown in fig. 1 below:

example usecase with sensor values

Fig. 1: Example usecase for such “sparklines” on the command-line, showing IoT sensor values (generating code not included here).

Due to limitations of available Unicode characters this works best when all values are positive. And even then true sparklines that look more like lines and less like bars are a real challenge, because they would need multiple characters with a single horizontal line on different vertical positions. This would work only with a dedicated font, which is way beyond the scope of this tool and which would significantly complicate its usage. So we stick to these characters: “▁▂▃▄▅▆▇█”, and use a blank for missing values.

This code was tested ok at some point for Python 2.6 and 2.7, but no longer supports Python 2 after it reached end-of-life. Now Python 3.5 to 3.8 as well as PyPy 3 are all tested via Travis-CI.

Sample output

This is a recorded sample session illustrating how to use sparklines (as GitHub doesn’t render embedded Asciinema recordings you’ll see here an image pointing to the respective asciicast):

https://asciinema.org/a/5xwfvcrrk09fy3ml3a8n67hep.png

Here is some example output on the command-line (please note that in some browsers the vertical alignment of these block characters might be displayed slightly wrong, the same effect can be seen for other repos referenced below):

Examples for the code below:

$ sparklines 2 7 1 8 2 8 1 8
▂▇▁█▂█▁█
$ echo 2 7 1 8 2 8 1 8 | sparklines
▂▇▁█▂█▁█
$ sparklines < numbers.txt
▂▇▁█▂█▁█
$ sparklines 0 2. 1e0
▁█▅

Installation

You can install this package using pip install sparklines from the Python Package Index. You can also clone this repository and install it via python setup.py install or pip install -e .. After installing, you will have access system-wide (or in your virtualenv if you have used that) to sparklines, programmatically as well as via a command-line tool with the same name.

Test

To run the test suite, download and unpack this repository or clone it, run pip inastall pytest, and run the command pytest tests in the unpacked archive. from the downloaded repository’s root folder.

Usage

Please note that the samples below might look a little funky (misaligned or even colored) in some browsers, but it should be totally fine when you print this in your terminal, Python or IPython session or your Python IDE of choice. Figure 2 below might show better what you should expect than the copied sample code thereafter:

example interactive invocation

Fig. 2: Example invocation from a Python and an IPython session.

Command-Line

Here are two sample invocations from the command-line, copied into this README:

$ sparklines 1 2 3 4 5.0 null 3 2 1
▁▃▅▆█ ▅▃▁

$ sparklines -n 2 1 2 3 4 5.0 null 3 2 1
  ▁▅█ ▁
▁▅███ █▅▁

Programmatic

And here are sample invocations from interactive Python sessions, copied into this README. The main function to use programmatically is sparklines.sparklines():

In [1]: from sparklines import sparklines

In [2]: for line in sparklines([1, 2, 3, 4, 5.0, None, 3, 2, 1]):
   ...:     print(line)
   ...:
▁▃▅▆█ ▅▃▁

In [3]: for line in sparklines([1, 2, 3, 4, 5.0, None, 3, 2, 1], num_lines=2):
    print(line)
   ...:
  ▁▅█ 
▁▅███ █▅▁

References

This code was inspired by Zach Holman’s spark, converted to a Python module by Kenneth Reitz as spark.py and by RegKrieg to a Python package named pysparklines. And Roger Allen provides an even shorter spark.py.

But since it is so short and easy to code in Python we can add a few nice extra features I was missing, like:

  • increasing resolution with multiple output lines per sparkline

  • showing gaps in input numbers for missing data

  • issuing warnings for negative values (allowed, but misleading)

  • highlighting values exceeding some threshold with a different color (if termcolor package is available)

  • wrapping long sparklines at some max. length

  • (todo) adding separator characters like : at regular intervals

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

sparklines-0.5.0.tar.gz (25.0 kB view details)

Uploaded Source

Built Distribution

sparklines-0.5.0-py3-none-any.whl (23.1 kB view details)

Uploaded Python 3

File details

Details for the file sparklines-0.5.0.tar.gz.

File metadata

  • Download URL: sparklines-0.5.0.tar.gz
  • Upload date:
  • Size: 25.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.9.19

File hashes

Hashes for sparklines-0.5.0.tar.gz
Algorithm Hash digest
SHA256 069e48633fc1af354e9bbdfd0a644c1279d6632a572446aa9d741105f1177000
MD5 1f5ce00c2c9ff57d38ce5f4a856f5441
BLAKE2b-256 4210a55ffb7b5ae130804ebcb53ecbb711a902403fe7efe67739aceeb4daf8f5

See more details on using hashes here.

File details

Details for the file sparklines-0.5.0-py3-none-any.whl.

File metadata

  • Download URL: sparklines-0.5.0-py3-none-any.whl
  • Upload date:
  • Size: 23.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.9.19

File hashes

Hashes for sparklines-0.5.0-py3-none-any.whl
Algorithm Hash digest
SHA256 436861df6382a5cf663ea9e0a52dcb5d4f95f5111f8e01eb637eca529e3f6807
MD5 b19322b5793e84da9ae5b8a8ab4c2ced
BLAKE2b-256 e9daa58f65cdf8117ae679ee4ddb3ab502d6ded7be2616d13cb179151e045b94

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