Код: Выделить всё
import asyncio
import flet as ft
import requests
import time
class Countdown(ft.Text):
def __init__(self):
super().__init__()
self.running = False
self.value = "Connecting..."
self.icon = ft.Icon(ft.icons.SIGNAL_WIFI_STATUSBAR_NULL_OUTLINED)
def did_mount(self):
self.running = True
self.update()
self.page.run_task(self.update_timer)
def will_unmount(self):
self.running = False
async def update_timer(self):
while self.running:
try:
requests.get("http://www.google.com", timeout=3)
self.value = "Connected"
self.icon = ft.Icon(ft.icons.WIFI)
self.chiconke()
self.update()
print(self.icon)
except requests.ConnectionError:
self.value = "Disconnected"
self.icon = ft.icons.WIFI
self.update()
await asyncio.sleep(5)
def create_statusbar():
countdown = Countdown()
return ft.Container(
content=ft.Row(
[
ft.CupertinoListTile(
bgcolor_activated=ft.colors.AMBER_ACCENT,
leading=countdown.icon,
title=countdown,
width=180,
),
ft.CupertinoListTile( # Example: API status
bgcolor_activated=ft.colors.AMBER_ACCENT,
leading=ft.Icon(name=ft.icons.API),
title=ft.Text(countdown.icon),
subtitle=ft.Text("Checking..."), # Placeholder for dynamic update
width=150,
),
ft.CupertinoListTile( # Example: API status
bgcolor_activated=ft.colors.AMBER_ACCENT,
leading=ft.Icon(name=ft.icons.API),
title=ft.Text("API Status"),
subtitle=ft.Text("Checking..."), # Placeholder for dynamic update
width=150,
),
ft.CupertinoListTile( # Example: API status
bgcolor_activated=ft.colors.AMBER_ACCENT,
leading=ft.Icon(name=ft.icons.API),
title=ft.Text("API Status"),
subtitle=ft.Text("Checking..."), # Placeholder for dynamic update
width=150,
),
],
),
border=ft.border.all(1, ft.colors.GREY_700),
border_radius=ft.border_radius.all(10),
)
Подробнее здесь: https://stackoverflow.com/questions/784 ... n-in-async