willmcgugan/rich

中文 readme Rich is a Python library for rich text and beautiful formatting in the terminal. The Rich API makes it easy to add color and style to terminal...
willmcgugan/rich




中文 readme

Rich is a Python library for rich textual jabber and supreme formatting within the terminal.

The Rich API makes it easy to add shade and magnificence to terminal output. Rich can even render somewhat tables, growth bars, markdown, syntax highlighted provide code, tracebacks, and additional — out of the sector.

For a video introduction to Rich take a look at calmcode.io by @fishnets88.

Compatibility

Rich works with Linux, OSX, and Dwelling windows. Perfect shade / emoji works with unique Dwelling windows Terminal, traditional terminal is specific to 8 colors.

Rich also works with Jupyter notebooks with out a extra configuration required.

Inserting in

Set up with pip or your popular PyPi package manager.

pip set up rich

Rich print characteristic

To without distress add rich output to your utility, you can import the rich print technique, which has the the same signature because the builtin Python characteristic. Achieve that:

from rich import print

print("Hi there, [bold magenta]World[/bold magenta]!", ":vampire:", locals())

The usage of the Console

For added protect watch over over rich terminal jabber, import and invent a Console object.

from rich.console import Console

console = Console()

The Console object has a print technique which has an intentionally equivalent interface to the builtin print characteristic. Right here’s an example of exercise:

console.print("Hi there", "World!")

As you’ll depend on, this would print "Hi there World!" to the terminal. Demonstrate that unlike the builtin print characteristic, Rich will note-wrap your textual jabber to compare within the terminal width.

There are about a systems of including shade and magnificence to your output. You presumably can situation a style for the total output by including a style keyword argument. Right here’s an example:

console.print("Hi there", "World!", style="courageous purple")

The output would possibly be one thing bask in the following:

That is good-hunting for styling a line of textual jabber at a time. For added finely grained styling, Rich renders a diversified markup which is equivalent in syntax to bbcode. Right here’s an example:

console.print("The set there is a [bold cyan]Will[/bold cyan] there [u]is[/u] a [i]come[/i].")

Console logging

The Console object has a log() technique which has a equivalent interface to print(), nonetheless also renders a column for the unique time and the file and line which made the resolution. By default Rich will raise out syntax highlighting for Python structures and for repr strings. If you log a series (i.e. a dict or a listing) Rich will somewhat print it in declare that it suits within the on hand dwelling. Right here’s an example of nearly all these aspects.

from rich.console import Console
console = Console()

test_data = [
    {"jsonrpc": "2.0", "method": "sum", "params": [None, 1, 2, 4, False, True], "identification": "1",},
    {"jsonrpc": "2.0", "technique": "notify_hello", "params": [7]},
    {"jsonrpc": "2.0", "technique": "subtract", "params": [42, 23], "identification": "2"},
]

def test_log():
    enabled = Fraudulent
    context = {
        "foo": "bar",
    }
    movies = ["Deadpool", "Rise of the Skywalker"]
    console.log("Hi there from", console, "!")
    console.log(test_data, log_locals=Perfect)


test_log()

The above produces the following output:

Demonstrate the log_locals argument, which outputs a table containing the local variables the set the log technique became as soon as called.

The log technique would possibly maybe also very successfully be old for logging to the terminal for lengthy operating functions corresponding to servers, nonetheless is also a extremely good debugging help.

Logging Handler

You presumably can even exercise the builtin Handler class to format and colorize output from Python’s logging module. Right here’s an example of the output:

Emoji

To insert an emoji in to console output space the title between two colons. Right here’s an example:

>>> console.print(":smiley: :vampire: :pile_of_poo: :thumbs_up: :raccoon:")
😃 🧛 💩 👍 🦝

Please exercise this selection wisely.

Tables

Rich can render flexible tables with unicode field characters. There is a unheard of diversity of formatting recommendations for borders, kinds, cell alignment and tons others.

The animation above became as soon as generated with table_movie.py within the examples directory.

Right here’s a less advanced table example:

from rich.console import Console
from rich.table import Column, Desk

console = Console()

table = Desk(show_header=Perfect, header_style="courageous magenta")
table.add_column("Date", style="unlit", width=12)
table.add_column("Title")
table.add_column("Manufacturing Value range", account for="appropriate")
table.add_column("Field Administrative heart", account for="appropriate")
table.add_row(
    "Dev 20, 2019", "Superstar Wars: The Upward push of Skywalker", "$275,000,000", "$375,126,118"
)
table.add_row(
    "Would per chance maybe well 25, 2018",
    "[red]Solo[/red]: A Superstar Wars Chronicle",
    "$275,000,000",
    "$393,151,347",
)
table.add_row(
    "Dec 15, 2017",
    "Superstar Wars Ep. VIII: The Final Jedi",
    "$262,000,000",
    "[bold]$1,332,539,889[/bold]",
)

console.print(table)

This produces the following output:

Demonstrate that console markup is rendered within the the same became as soon as as print() and log(). In actuality, the rest that is renderable by Rich would possibly maybe be integrated within the headers / rows (even other tables).

The Desk class is orderly ample to resize columns to compare the on hand width of the terminal, wrapping textual jabber as required. Right here’s the the same example, with the terminal made smaller than the table above:

Progress Bars

Rich can render a pair of flicker-free growth bars to music lengthy-operating tasks.

For overall usage, wrap any sequence within the music characteristic and iterate over the outcome. Right here’s an example:

from rich.growth import music

for step in music(vary(100)):
    do_step(step)

It be no longer necessary harder to add a pair of growth bars. Right here’s an example taken from the docs:

The columns would possibly maybe be configured to reveal any crucial points you will want. Constructed-in columns encompass share total, file size, file trail, and time last. Right here’s one other example showing a download in growth:

To strive this out yourself, take a look at examples/downloader.py which will download a pair of URLs concurrently whereas showing growth.

Columns

Rich can render jabber in neat columns with equal or optimum width. Right here’s a extremely overall clone of the (MacOS / Linux) ls reveal which displays a directory itemizing in columns:

import os

from rich import print
from rich.columns import Columns

directory = os.listdir(sys.argv[1])
print(Columns(directory))

The following screenshot is the output from the columns example which displays data pulled from an API in columns:

Markdown

Rich can render markdown and does an reasonable job of translating the formatting to the terminal.

To render markdown import the Markdown class and invent it with a string containing markdown code. Then print it to the console. Right here’s an example:

from rich.console import Console
from rich.markdown import Markdown

console = Console()
with commence("README.md") as readme:
    markdown = Markdown(readme.be taught())
console.print(markdown)

This would possibly occasionally invent output one thing bask in the following:

Syntax Highlighting

Rich uses the pygments library to enforce syntax highlighting. Utilization is akin to rendering markdown; invent a Syntax object and print it to the console. Right here’s an example:

from rich.console import Console
from rich.syntax import Syntax

my_code = '''
def iter_first_last(values: Iterable[T]) -> Iterable[Tuple[bool, bool, T]]: 
    """Iterate and generate a tuple with a flag for first and final tag."""
    iter_values = iter(values)
    strive: 
        previous_value = subsequent(iter_values)
    in addition to StopIteration: 
        return
    first = Perfect
    for tag in iter_values: 
        yield first, Fraudulent, previous_value
        first = Fraudulent
        previous_value = tag
    yield first, Perfect, previous_value
'''
syntax = Syntax(my_code, "python", theme="monokai", line_numbers=Perfect)
console = Console()
console.print(syntax)

This would possibly occasionally invent the following output:

Tracebacks

Rich can render supreme tracebacks which would possibly maybe be easier to be taught and reveal extra code than customary Python tracebacks. You presumably can situation Rich because the default traceback handler so all uncaught exceptions would possibly be rendered by Rich.

Right here’s what it looks bask in on OSX (equivalent on Linux):

Right here’s what it looks bask in on Dwelling windows:

Peek the rich traceback documentation for the runt print.

Learn Extra

Categories
Internet of Things
122 Comments on this post.
  • Fbsbhaify
    27 November 2020 at 10:55 am
  • Fgsreete
    28 November 2020 at 10:37 pm
  • Fnrdhaify
    29 November 2020 at 8:20 am
  • JgscMoume
    29 November 2020 at 4:46 pm
  • Dvscreete
    29 November 2020 at 5:54 pm
  • Caseyphync
    30 November 2020 at 10:01 am
  • Fsfhaify
    30 November 2020 at 3:01 pm
  • Kwgdcory
    30 November 2020 at 6:18 pm
  • Fnsbhaify
    1 December 2020 at 1:04 pm
  • Fnrhhaify
    1 December 2020 at 6:09 pm
  • Fqfhaify
    2 December 2020 at 6:23 pm
  • Ftghhaify
    2 December 2020 at 8:26 pm
  • RodneythOva
    3 December 2020 at 7:47 am
  • RodneythOva
    3 December 2020 at 3:09 pm
  • RodneythOva
    3 December 2020 at 10:29 pm
  • RodneythOva
    4 December 2020 at 5:03 am

    prescription without a doctor’s prescription Amoxil levitra without a doctor prescription

  • RodneythOva
    4 December 2020 at 10:32 am
  • RodneythOva
    4 December 2020 at 4:51 pm

    erectyle dysfunction generic drugs over the counter ed medication

  • RodneythOva
    4 December 2020 at 11:11 pm
  • RodneythOva
    5 December 2020 at 5:25 am

    where to bay cialis (tadalafil) pills 80mg cialis coupon cialis lowest price

  • RodneythOva
    5 December 2020 at 12:57 pm
  • EdwardThofe
    7 December 2020 at 1:14 am
  • EdwardThofe
    7 December 2020 at 9:06 am
  • Fgnsreete
    8 December 2020 at 9:08 pm
  • RichardTolve
    9 December 2020 at 1:30 pm
  • Dvncreete
    9 December 2020 at 4:02 pm
  • RichardTolve
    9 December 2020 at 9:38 pm
  • RichardTolve
    10 December 2020 at 6:08 am
  • RichardTolve
    10 December 2020 at 2:22 pm
  • RichardTolve
    10 December 2020 at 9:35 pm
  • RichardTolve
    11 December 2020 at 3:23 am
  • JgsvMoume
    11 December 2020 at 4:42 am
  • Fbsbhaify
    11 December 2020 at 5:24 am
  • RichardTolve
    11 December 2020 at 9:15 am
  • Fsfghaify
    11 December 2020 at 2:16 pm
  • Khedcory
    12 December 2020 at 8:16 am

    need to get samples of viagra without a perscription buy viagra oneline buy viagra with dapoxetine

  • ThomasJef
    12 December 2020 at 8:49 am
  • ThomasJef
    12 December 2020 at 4:14 pm
  • ThomasJef
    12 December 2020 at 11:54 pm
  • Fqbfhaify
    13 December 2020 at 3:56 am
  • Fhsnreete
    13 December 2020 at 7:07 pm
  • Fgrshaify
    14 December 2020 at 1:58 am
  • Dvnjreete
    14 December 2020 at 1:34 pm
  • JamesBew
    15 December 2020 at 9:10 am
  • JamesBew
    15 December 2020 at 5:39 pm
  • JamesBew
    16 December 2020 at 2:35 am
  • JamesBew
    16 December 2020 at 11:59 am
  • JamesBew
    16 December 2020 at 8:59 pm
  • HydraOnionBest
    16 December 2020 at 9:38 pm
  • JamesBew
    17 December 2020 at 4:10 am
  • GregoryPet
    17 December 2020 at 8:34 am

    ed treatment options cause of ed prescription drugs online without

  • GregoryPet
    17 December 2020 at 5:33 pm
  • GregoryPet
    18 December 2020 at 2:58 am
  • GregoryPet
    18 December 2020 at 12:36 pm
  • GregoryPet
    18 December 2020 at 8:37 pm
  • GregoryPet
    19 December 2020 at 2:54 am
  • GregoryPet
    19 December 2020 at 9:19 am
  • MichaelNeoro
    21 December 2020 at 9:51 am
  • hey
    21 December 2020 at 1:59 pm

    Taxi moto line
    128 Rue la Boétie
    75008 Paris
    +33 6 51 612 712  

    Taxi moto paris

    I like it when individuals come together and share opinions.

    Great site, continue the good work!

  • MichaelNeoro
    21 December 2020 at 6:47 pm
  • https://viagmmy.com/
    23 December 2020 at 4:37 am

    break 20mg cialis half
    viagra 75mg
    cialis cost increase Cag wef

  • Warrenfax
    23 December 2020 at 1:22 pm
  • Warrenfax
    23 December 2020 at 8:18 pm
  • Warrenfax
    24 December 2020 at 3:30 am
  • Warrenfax
    24 December 2020 at 1:11 pm

    can i buy acyclovir cream over the counter buy acyclovir can i buy valtrex over the counter

  • Warrenfax
    24 December 2020 at 11:02 pm
  • Warrenfax
    25 December 2020 at 7:07 am
  • Warrenfax
    25 December 2020 at 1:21 pm

    amoxicillin 500 mg brand name antibiotic what is antibiotic resistance

  • Haroldnum
    29 December 2020 at 3:32 am
  • Haroldnum
    29 December 2020 at 11:37 am
  • Haroldnum
    29 December 2020 at 6:27 pm
  • Haroldnum
    30 December 2020 at 1:09 am
  • Haroldnum
    30 December 2020 at 7:52 am
  • Kennethdaype
    3 January 2021 at 9:38 am
  • where to buy cialis 5mg online
    3 January 2021 at 1:49 pm
  • Kennethdaype
    3 January 2021 at 5:06 pm
  • genuine viagra
    5 January 2021 at 2:36 am
  • howard stern viagra
    6 January 2021 at 5:00 pm
  • https://doctorpbn.com/
    8 January 2021 at 1:48 pm

    buy cialis online australia
    viagra 75mg
    5mg cialis daily use Cag wef

  • Aqwshaify
    9 January 2021 at 7:22 am
  • Nllpreete
    9 January 2021 at 10:47 am
  • Khthcory
    9 January 2021 at 12:00 pm
  • JlloMoume
    9 January 2021 at 4:53 pm
  • Fgvdreete
    10 January 2021 at 10:55 am
  • Kennethshole
    11 January 2021 at 7:45 am
  • Jtmfhaify
    11 January 2021 at 4:36 pm
  • Kennethshole
    11 January 2021 at 5:40 pm
  • Lokuhaify
    11 January 2021 at 8:53 pm
  • cialis tablete za potenciju
    13 January 2021 at 7:56 am
  • Rickyprand
    15 January 2021 at 2:18 am
  • sildenafil generika rezeptfrei kaufen
    15 January 2021 at 6:36 am
  • Rickyprand
    15 January 2021 at 8:42 am
  • Rickyprand
    15 January 2021 at 3:30 pm
  • Rickyprand
    15 January 2021 at 8:52 pm
  • Rickyprand
    16 January 2021 at 2:02 am
  • Rickyprand
    16 January 2021 at 7:08 am
  • Rickyprand
    16 January 2021 at 12:02 pm
  • SamuelMeque
    17 January 2021 at 7:41 am
  • SamuelMeque
    17 January 2021 at 12:31 pm
  • SamuelMeque
    17 January 2021 at 5:25 pm
  • SamuelMeque
    17 January 2021 at 9:56 pm
  • SamuelMeque
    18 January 2021 at 3:58 am
  • SamuelMeque
    18 January 2021 at 9:02 am
  • Nlbxreete
    18 January 2021 at 4:02 pm

    european cialis cialis & dapoxitine cialis shipping [url=http://cialijomen.com/]buy cialis online[/url] ’

  • Aqbvhaify
    19 January 2021 at 2:29 am

    what\’s the difference in viagra generic viagra 100 4 pack viagra on line [url=http://acialaarx.com/]where to buy viagra in tucson without prescription in person[/url] ’

  • Kbctcory
    19 January 2021 at 7:13 am

    canadian meds cialis cialis at costco cialis and paypal [url=http://mycialedst.com/]cialis 20mg price[/url] ’

  • StephenOdowl
    19 January 2021 at 10:38 am
  • Elovigo
    19 January 2021 at 11:50 am
  • JvcbxMoume
    19 January 2021 at 12:03 pm

    viagra and cialis discount cialis no prescription uk cialis where to buy [url=http://phrcialiled.com/]cialis dose[/url] ’

  • Fbdhreete
    19 January 2021 at 3:22 pm

    paypal cialis tadalafil best price for cialis cialis online [url=http://21cialismen.com/]price of cialis[/url] ’

  • StephenOdowl
    19 January 2021 at 5:49 pm
  • Jbdchaify
    21 January 2021 at 10:04 am
  • Lcxhaify
    21 January 2021 at 2:10 pm

    viagra prices walgreens professional viagra online viagra 100mg professional [url=http://genqpviag.com/]super force viagra[/url] ’

  • Fsbxhaify
    22 January 2021 at 12:29 pm

    how much is viagra in tuajuana with a prescription generic viagra next day best generic viagra [url=http://genericrxxx.com/]viagra canada[/url] ’

  • GerardBig
    22 January 2021 at 2:43 pm
  • GerardBig
    22 January 2021 at 10:04 pm
  • Nmilreete
    23 January 2021 at 11:59 am

    viagra natural para hombres generic viagra viagra on without insurance [url=http://xz-pharmacyonline.com/en/career-opportunities.html]generic viagra[/url] ’

  • Ahmdhaify
    23 January 2021 at 9:39 pm

    generic cialis 20mg generic cialis price of cialis [url=http://cialmenshoprx.com/]prolonged effects of cialis[/url] ’

  • Kbbtcory
    23 January 2021 at 11:59 pm

    cialis price canada Female Cialis Soft cialis professional wikipedia [url=https://xz-pharmacyonline.com]panacea pharmacy[/url] ’

  • JnhMoume
    24 January 2021 at 3:58 am
  • Fbgreete
    24 January 2021 at 10:53 am

    viagra and celexa viagra lowest priced professional brand viagra info [url=https://canadianpharmacy-usx.com/organic.htm]buy viagra australia online[/url] ’

  • ChrisDut
    26 January 2021 at 1:51 pm
  • Leave a Reply

    *

    *

    RELATED BY