-
Notifications
You must be signed in to change notification settings - Fork 0
/
optionlist_from_api.py
40 lines (31 loc) · 1.03 KB
/
optionlist_from_api.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import asyncio
import httpx
from textual import work
from textual.app import App, ComposeResult
from textual.widgets import OptionList
from textual.widgets.option_list import Option
class OptionListAPIApp(App[None]):
CSS = """
OptionList {
min-height: 3;
}
"""
def compose(self) -> ComposeResult:
yield OptionList()
def on_mount(self) -> None:
# self.dark = False
option_list = self.query_one(OptionList)
option_list.loading = True
self.load_data(option_list)
@work
async def load_data(self, option_list: OptionList) -> None:
# REMOVE THE SLEEP IN PRODUCTION
await asyncio.sleep(2)
async with httpx.AsyncClient() as client:
r = await client.get("https://jsonplaceholder.typicode.com/users")
for user in r.json():
option_list.add_option(
Option(f"{user['name']} [underline]({user['email']})[/]")
)
option_list.loading = False
OptionListAPIApp().run()