-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7eb10f0
commit 6bb9c76
Showing
8 changed files
with
122 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
Columns | ||
======= | ||
|
||
Rich can render text or other Rich renderables in neat columns with the :class:`~rich.columns.Columns` class. To use, construct a Columns instance with an iterable of renderables and print it to the Console. | ||
|
||
The following example is a very basic clone of the `ls` command in OSX / Linux to list directory contents:: | ||
|
||
import os | ||
import sys | ||
|
||
from rich import print | ||
from rich.columns import Columns | ||
|
||
if len(sys.argv) < 2: | ||
print("Usage: python columns.py DIRECTORY") | ||
else: | ||
directory = os.listdir(sys.argv[1]) | ||
columns = Columns(directory, equal=True, expand=True) | ||
print(columns) | ||
|
||
|
||
See `columns.py <https://github.com/willmcgugan/rich/blob/master/examples/columns.py>`_ for an example which outputs columns with more than just text. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
rich.columns | ||
============ | ||
|
||
.. automodule:: rich.columns | ||
:members: | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
from rich import print | ||
from rich.columns import Columns | ||
from rich.panel import Panel | ||
|
||
import json | ||
from urllib.request import urlopen | ||
|
||
|
||
users = json.loads(urlopen("https://randomuser.me/api/?results=30").read())["results"] | ||
print(users) | ||
|
||
user_renderables = [ | ||
Panel( | ||
f"[b]{user['name']['first']} {user['name']['last']}[/b]\n[yellow]{user['location']['country']}", | ||
expand=False, | ||
) | ||
for user in users | ||
] | ||
|
||
print(Columns(user_renderables)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import os | ||
import sys | ||
|
||
from rich import print | ||
from rich.columns import Columns | ||
|
||
if len(sys.argv) < 2: | ||
print("Usage: python columns.py DIRECTORY") | ||
else: | ||
directory = os.listdir(sys.argv[1]) | ||
columns = Columns(directory, equal=True, expand=True) | ||
print(columns) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import io | ||
|
||
from rich.columns import Columns | ||
from rich.console import Console | ||
|
||
COLUMN_DATA = [ | ||
"Ursus americanus", | ||
"American buffalo", | ||
"Bison bison", | ||
"American crow", | ||
"Corvus brachyrhynchos", | ||
"American marten", | ||
"Martes americana", | ||
"American racer", | ||
"Coluber constrictor", | ||
"American woodcock", | ||
"Scolopax minor", | ||
"Anaconda (unidentified)", | ||
"Eunectes sp.", | ||
"Andean goose", | ||
"Chloephaga melanoptera", | ||
"Ant", | ||
"Anteater, australian spiny", | ||
"Tachyglossus aculeatus", | ||
"Anteater, giant", | ||
"Myrmecophaga tridactyla", | ||
] | ||
|
||
|
||
def render(): | ||
console = Console(file=io.StringIO(), width=100) | ||
columns = Columns(COLUMN_DATA) | ||
console.print(columns) | ||
console.print() | ||
columns.column_first = True | ||
console.print(columns) | ||
console.print() | ||
columns.right_to_left = True | ||
console.print(columns) | ||
console.print() | ||
columns.equal = True | ||
columns.expand = True | ||
console.print(columns) | ||
console.print() | ||
render_result = console.file.getvalue() | ||
return render_result | ||
|
||
|
||
def test_render(): | ||
expected = "Ursus americanus American buffalo Bison bison American crow \nCorvus brachyrhynchos American marten Martes americana American racer \nColuber constrictor American woodcock Scolopax minor Anaconda (unidentified)\nEunectes sp. Andean goose Chloephaga melanoptera Ant \nAnteater, australian spiny Tachyglossus aculeatus Anteater, giant Myrmecophaga tridactyla\n\nUrsus americanus Corvus brachyrhynchos Coluber constrictor Eunectes sp. \nAnteater, australian spiny American buffalo American marten American woodcock \nAndean goose Tachyglossus aculeatus Bison bison Martes americana \nScolopax minor Chloephaga melanoptera Anteater, giant American crow \nAmerican racer Anaconda (unidentified) Ant Myrmecophaga tridactyla\n\nEunectes sp. Coluber constrictor Corvus brachyrhynchos Ursus americanus \nAmerican woodcock American marten American buffalo Anteater, australian spiny\nMartes americana Bison bison Tachyglossus aculeatus Andean goose \nAmerican crow Anteater, giant Chloephaga melanoptera Scolopax minor \nMyrmecophaga tridactyla Ant Anaconda (unidentified) American racer \n\nMartes americana American crow Ursus americanus \nAnt Eunectes sp. American woodcock \nCorvus brachyrhynchos American buffalo Anteater, giant \nAndean goose Scolopax minor American racer \nBison bison Myrmecophaga tridactyla Anteater, australian spiny \nAnaconda (unidentified) Coluber constrictor American marten \n Tachyglossus aculeatus Chloephaga melanoptera \n\n" | ||
assert render() == expected | ||
|
||
|
||
if __name__ == "__main__": | ||
result = render() | ||
print(result) | ||
print(repr(result)) |