Format rich Text
Project description
rich_format
rich_format
replicates python string formatting for rich Text
instances adding the possibility to substitute also with Text
.
>>> from rich.console import Console
>>> from rich.text import Text
>>> import rich_format
>>> console = Console()
>>> text = Text("Hello {name}",style="red on white")
>>> formatted_text = t.format(name=Text("pom11"))
>>> console.print(text)
Hello {name}
>>> console.print(formatted_text)
Hello pom11
See more examples running rich_format.test
or inspect this.
Usage
First you need to import Text
from rich so that rich_format
works.
from rich.text import Text
import rich_format
Textual
You can use rich_format
in textual
to format easier reactive
in custom widgets
rich_format.demo
from textual.reactive import reactive
from textual.app import App, ComposeResult
from textual.widgets import Footer
from textual.widgets import Static
from rich.text import Text
import rich_format
import random
city = [
Text.from_markup("from [blue on red]London[/]"),
Text.from_markup("from [magenta on blue]New York[/]"),
Text.from_markup("from [green]Bucharest[/]"),
Text.from_markup("from [red on white]Tokyo[/]")
]
class CustomHeader(Static):
banner : Text = reactive(Text(""))
world : str = reactive("")
def __init__(self, template: str) -> None:
self.template = template
super().__init__()
def watch_banner(self, banner: Text) -> None:
self.update(banner)
def watch_world(self, world: str) -> None:
self.banner = Text(self.template).format(world=world)
class DemoApp(App):
BINDINGS = [
("q","quit","Quit"),
("h", "toggle_header", "Random header")
]
def compose(self) -> ComposeResult:
yield CustomHeader(template="Hello {world}")
yield Footer()
def action_toggle_header(self) -> None:
widget = self.query_one(CustomHeader)
widget.world = random.choice(city)
if __name__ == "__main__":
app = DemoApp()
app.run()
Installation
Stable release - pypi
To install rich_format
run this command in your terminal
pip install rich_format
From sources
The sources for rich_format can be downloaded from Github
- clone the public repository
git clone https://github.com/pom11/rich_format
- install from source
python setup.py install
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
rich_format-0.0.4.tar.gz
(5.8 kB
view details)
Built Distribution
File details
Details for the file rich_format-0.0.4.tar.gz
.
File metadata
- Download URL: rich_format-0.0.4.tar.gz
- Upload date:
- Size: 5.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.8.3 requests/2.28.1 setuptools/65.6.3 requests-toolbelt/0.9.1 tqdm/4.64.1 CPython/3.7.13
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 907a2de05c96e741d8bdf9a33cdfc477e94611422a1a89ef7bf8796e2b34a4a5 |
|
MD5 | fe2885c8240d2c8da4804df51485e15e |
|
BLAKE2b-256 | 19e8931829637bf9d01e6d7edf4aed8a03592c80c3ceb01a6eec8d11f98ee460 |
File details
Details for the file rich_format-0.0.4-py3-none-any.whl
.
File metadata
- Download URL: rich_format-0.0.4-py3-none-any.whl
- Upload date:
- Size: 7.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.8.3 requests/2.28.1 setuptools/65.6.3 requests-toolbelt/0.9.1 tqdm/4.64.1 CPython/3.7.13
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | f4739bfb55f0c0d8bc247a8301c67d8f4eb59ad44b59a00633baa1372d05dff4 |
|
MD5 | 015ac88acf8338c812153bb114637e61 |
|
BLAKE2b-256 | 6ccbf51320ab70b12cc862ac45ee0f24936d08c44aa48c344fa2a5283a79aa9b |