pawkanarek commited on
Commit
a0ea5d6
·
1 Parent(s): 5c033db

add caching

Browse files
Files changed (1) hide show
  1. app.py +21 -11
app.py CHANGED
@@ -2,6 +2,11 @@ import bittensor as bt
2
  from substrateinterface import Keypair
3
  import gradio as gr
4
  import pandas as pd
 
 
 
 
 
5
 
6
 
7
  def get_incentive_percent_for_key(key, sn, incentive, substrate) -> tuple[float, int]:
@@ -43,29 +48,34 @@ def fetch_incentive_data() -> pd.DataFrame:
43
  address_link = f"[{owner_hk}](https://taostats.io/hotkey/{owner_hk})"
44
  data.append([sn_link, f"{hk_incentive:.2f}%", hk_uid, "Hotkey", address_link])
45
  break
46
-
47
- df = pd.DataFrame(data, columns=["Subnet", "Incentive", "UID", "Key Type", "Address"]) #type: ignore
 
48
  return df
49
 
50
 
 
 
 
 
 
 
 
 
51
  with gr.Blocks(title="Bittensor Subnet Incentives") as demo:
52
  gr.Markdown(
53
  """
54
  # Bittensor Subnet Burn Dashboard
55
  This dashboard displays the burn percentage set by subnet owners for miners.
56
- Click the 'Refresh' button to fetch the latest data.
57
  """
58
  )
59
 
60
- refresh_button = gr.Button("Refresh Data")
61
  output_df = gr.DataFrame(
62
- headers=["Subnet", "Burn", "UID", "Key", "Address"],
63
- datatype=["markdown", "str", "number", "str", "markdown"],
64
- label="Subnet Incentive Data",
65
  interactive=False,
66
- row_count=(60, "dynamic"),
67
- col_count=(5, "fixed")
68
  )
69
-
70
- demo.load(fetch_incentive_data, None, output_df)
71
  demo.launch()
 
2
  from substrateinterface import Keypair
3
  import gradio as gr
4
  import pandas as pd
5
+ import time
6
+
7
+ # uga-buga caching
8
+ g_cached_data: pd.DataFrame | None = None
9
+ g_last_fetch_time = 0.0
10
 
11
 
12
  def get_incentive_percent_for_key(key, sn, incentive, substrate) -> tuple[float, int]:
 
48
  address_link = f"[{owner_hk}](https://taostats.io/hotkey/{owner_hk})"
49
  data.append([sn_link, f"{hk_incentive:.2f}%", hk_uid, "Hotkey", address_link])
50
  break
51
+
52
+ data = [(i+1, *d) for i, d in enumerate(data)]
53
+ df = pd.DataFrame(data, columns=["#", "Subnet", "Burn", "UID", "Key", "Address"]) # type: ignore
54
  return df
55
 
56
 
57
+ def get_cached_data() -> pd.DataFrame:
58
+ global g_cached_data, g_last_fetch_time
59
+ if g_cached_data is None or (time.time() - g_last_fetch_time) > 1200: # 20 min
60
+ g_last_fetch_time = time.time()
61
+ g_cached_data = fetch_incentive_data()
62
+ return g_cached_data
63
+
64
+
65
  with gr.Blocks(title="Bittensor Subnet Incentives") as demo:
66
  gr.Markdown(
67
  """
68
  # Bittensor Subnet Burn Dashboard
69
  This dashboard displays the burn percentage set by subnet owners for miners.
 
70
  """
71
  )
72
 
 
73
  output_df = gr.DataFrame(
74
+ datatype=["number", "markdown", "str", "number", "str", "markdown"],
75
+ label="Subnet Burn Data",
 
76
  interactive=False,
77
+ row_count=(12, "dynamic"),
78
+ col_count=(6, "fixed")
79
  )
80
+ demo.load(get_cached_data, None, output_df)
 
81
  demo.launch()