Skip to main content

Yet Another Terminal Spinner

Project description

Yet Another Terminal Spinner for Python.

Yaspin provides a full-featured terminal spinner to show the progress during long-hanging operations.

https://raw.githubusercontent.com/pavdmyt/yaspin/master/gifs/demo.gif

It is easy to integrate into existing codebase by using it as a context manager or as a function decorator:

import time
from yaspin import yaspin

# Context manager:
with yaspin():
    time.sleep(3)  # time consuming code

# Function decorator:
@yaspin(text="Loading...")
def some_operations():
    time.sleep(3)  # time consuming code

some_operations()

Features

  • No external dependencies

  • Runs at all major CPython versions (2.6, 2.7, 3.3, 3.4, 3.5, 3.6), PyPy and PyPy3

  • Supports all (60+) spinners from cli-spinners

  • Supports all colors, highlights, attributes and their mixes from termcolor library

  • Easy to combine with other command-line libraries, e.g. prompt-toolkit

  • Flexible API, easy to integrate with existing code

  • Safe pipes and redirects:

$ python script_that_uses_yaspin.py > script.log
$ python script_that_uses_yaspin.py | grep ERROR

Installation

From PyPI using pip package manager:

pip install --upgrade yaspin

Or install the latest sources from GitHub:

pip install https://github.com/pavdmyt/yaspin/archive/master.zip

Usage

Basic Example

https://raw.githubusercontent.com/pavdmyt/yaspin/master/gifs/basic_example.gif
# -*- coding: utf-8 -*-
import time
from random import randint
from yaspin import yaspin

with yaspin(text="Loading", color="yellow") as spinner:
    time.sleep(2)  # time consuming code

    success = randint(0, 1)
    if success:
        spinner.ok("✅ ")
    else:
        spinner.fail("💥 ")

It is also possible to control spinner manually:

# -*- coding: utf-8 -*-
import time
from yaspin import yaspin

spinner = yaspin()
spinner.start()

time.sleep(3)  # time consuming tasks

spinner.stop()

Run any spinner from cli-spinners

https://raw.githubusercontent.com/pavdmyt/yaspin/master/gifs/cli_spinners.gif
# -*- coding: utf-8 -*-
import time
from yaspin import yaspin
from yaspin.spinners import Spinners

with yaspin(Spinners.earth, text="Earth") as sp:
    time.sleep(2)                # time consuming code

    # change spinner
    sp.spinner = Spinners.moon
    sp.text = "Moon"

    time.sleep(2)                # time consuming code

Any Colour You Like 🌈

https://raw.githubusercontent.com/pavdmyt/yaspin/master/gifs/basic_colors.gif
# -*- coding: utf-8 -*-
import time
from yaspin import yaspin

with yaspin(text="Colors!") as sp:
    # Support all basic termcolor text colors
    colors = ("red", "green", "yellow", "blue", "magenta", "cyan", "white")

    for color in colors:
        sp.color, sp.text = color, color
        time.sleep(1)

Advanced colors usage

https://raw.githubusercontent.com/pavdmyt/yaspin/master/gifs/advanced_colors.gif
# -*- coding: utf-8 -*-
import time
from yaspin import yaspin
from yaspin.spinners import Spinners
from yaspin.termcolor import colored

text = "Bold blink magenta spinner on cyan color"
# Support all termcolor features via simple closure
color_fn = lambda frame: colored(frame, "magenta", "on_cyan", attrs=["bold", "blink"])

with yaspin(Spinners.bouncingBall, text=text, color=color_fn):
    time.sleep(3)

Run any spinner you want

https://raw.githubusercontent.com/pavdmyt/yaspin/master/gifs/custom_spinners.gif
# -*- coding: utf-8 -*-
import time
from yaspin import yaspin, Spinner

# Compose new spinners with custom frame sequence and interval value
sp = Spinner(["😸", "😹", "😺", "😻", "😼", "😽", "😾", "😿", "🙀"], 200)

with yaspin(sp, text="Cat!"):
    time.sleep(3)  # cat consuming code :)

Change spinner properties on the fly

https://raw.githubusercontent.com/pavdmyt/yaspin/master/gifs/sp_properties.gif
# -*- coding: utf-8 -*-
import time
from yaspin import yaspin
from yaspin.spinners import Spinners

with yaspin(Spinners.noise, text="Noise spinner") as sp:
    time.sleep(2)

    sp.spinner = Spinners.arc  # spinner type
    sp.text = "Arc spinner"    # text along with spinner
    sp.color = "green"         # spinner color
    sp.right = True            # put spinner to the right
    sp.reverse = True          # reverse spin direction

    time.sleep(2)

Writing messages

https://raw.githubusercontent.com/pavdmyt/yaspin/master/gifs/write_text.gif

You should not write any message in the terminal using print while spinner is open. To write messages in the terminal without any collision with yaspin spinner, a .write() method is provided:

# -*- coding: utf-8 -*-
import time
from yaspin import yaspin

with yaspin(text="Downloading images", color="cyan") as sp:
    # task 1
    time.sleep(1)
    sp.write("> image 1 download complete")

    # task 2
    time.sleep(2)
    sp.write("> image 2 download complete")

    # finalize
    sp.ok("✔")

Integration with other libraries

https://raw.githubusercontent.com/pavdmyt/yaspin/master/gifs/hide_show.gif

Utilizing hide and show methods it is possible to toggle the display of the spinner in order to call custom methods that write to the terminal. This is helpful for allowing easy usage in other frameworks like prompt-toolkit. Using the powerful print_formatted_text function allows you even to apply HTML formats and CSS styles to the output:

# -*- coding: utf-8 -*-
from __future__ import print_function

import sys
import time

from yaspin import yaspin
from prompt_toolkit import HTML, print_formatted_text
from prompt_toolkit.styles import Style

# override print with feature-rich ``print_formatted_text`` from prompt_toolkit
print = print_formatted_text

# build a basic prompt_toolkit style for styling the HTML wrapped text
style = Style.from_dict({
    'msg': '#4caf50 bold',
    'sub-msg': '#616161 italic'
})


with yaspin(text='Downloading images') as sp:
    # task 1
    time.sleep(1)
    sp.hide()
    print(HTML(
        u'<b>></b> <msg>image 1</msg> <sub-msg>download complete</sub-msg>'
    ), style=style)
    sp.show()

    # task 2
    time.sleep(2)
    sp.hide()
    print(HTML(
        u'<b>></b> <msg>image 2</msg> <sub-msg>download complete</sub-msg>'
    ), style=style)
    sp.show()

    # finalize
    sp.ok()

More examples.

Development

Clone the repository:

git clone https://github.com/pavdmyt/yaspin.git

Install dev dependencies:

pip install -r requirements-dev.txt

Lint code:

make lint

Run tests:

make test

Contributing

  1. Fork it!

  2. Create your feature branch: git checkout -b my-new-feature

  3. Commit your changes: git commit -m 'Add some feature'

  4. Push to the branch: git push origin my-new-feature

  5. Submit a pull request

  6. Make sure tests are passing

License

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

yaspin-0.11.1.tar.gz (25.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

yaspin-0.11.1-py2.py3-none-any.whl (18.6 kB view details)

Uploaded Python 2Python 3

File details

Details for the file yaspin-0.11.1.tar.gz.

File metadata

  • Download URL: yaspin-0.11.1.tar.gz
  • Upload date:
  • Size: 25.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for yaspin-0.11.1.tar.gz
Algorithm Hash digest
SHA256 26cc21eaca8ad32ac7d60f56b4ef1f66db807fd303a7cd68a1ba77c508b3ee94
MD5 7385330779477aa26ae415020afa296e
BLAKE2b-256 153bea588561425cab302aa2681b5d26376d69c9f73e1f6361ef65d46fbc5d38

See more details on using hashes here.

File details

Details for the file yaspin-0.11.1-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for yaspin-0.11.1-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 e9c4812d8e9c9436c7907e3ce41276e0d8459cf010c1e12990459a776461ee98
MD5 e54da0d4bc7748c961124325b32a51a6
BLAKE2b-256 de9718cbc497ab2354f6ab20bda9a01aed4a83fb54de82c243d68290798cfc56

See more details on using hashes here.

Supported by

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