From 55c9f6a414a28d09b0862f9d787a0c4cadd61fa0 Mon Sep 17 00:00:00 2001 From: 3gg <3gg@shellblade.net> Date: Wed, 9 Aug 2023 08:40:29 -0700 Subject: Add footer with update time. --- market.py | 41 +++++++++++++++++++++++++++-------------- 1 file changed, 27 insertions(+), 14 deletions(-) diff --git a/market.py b/market.py index 6ac95ee..d5f3c76 100755 --- a/market.py +++ b/market.py @@ -1,13 +1,14 @@ #!/usr/bin/python import argparse from collections import namedtuple +from datetime import datetime import requests import os import sys import time from textual.app import App, ComposeResult -from textual.widgets import DataTable +from textual.widgets import DataTable, Label # Rapid API key must be set in the environment. RAPIDAPI_KEY = os.environ.get('RAPIDAPI_KEY') @@ -212,6 +213,11 @@ def update_state(t: float, updaters: list[Updater], state: State) -> State: class MarketApp(App): TITLE = "Market Watch" + CSS = """ + #footer { + dock: bottom; + } + """ def __init__(self, updaters: list[Updater]): super().__init__() @@ -220,9 +226,29 @@ class MarketApp(App): self.updaters = updaters self.min_update_delay = min([updater.delay for updater in updaters]) + def compose(self) -> ComposeResult: + table = DataTable() + table.show_cursor = False + self.table = table + yield table + + footer = Label(id="footer") + self.footer = footer + yield footer + + def on_mount(self): + self.state = load_state(STATE_FILE) + self.update() + self.render() + self.set_interval(self.min_update_delay, self.update) + def render(self): assert self.state is not None assert self.table is not None + assert self.footer is not None + + update_time = datetime.now() + self.footer.update(f"Last update: {update_time}") # Stock/ex | Price | Change # xyz | xxx | xxx @@ -236,19 +262,6 @@ class MarketApp(App): for exchange in self.state.exchanges: table.add_row(format_exchange_name(exchange), exchange.rate, "", "") - def compose(self) -> ComposeResult: - self.state = load_state(STATE_FILE) - - table = DataTable() - table.show_cursor = False - self.table = table - yield table - - self.render() - - self.update() - self.set_interval(self.min_update_delay, self.update) - def update(self) -> None: t = time.time() self.state = update_state(t, self.updaters, self.state) -- cgit v1.2.3