hklein42 commited on
Commit
ed1d227
1 Parent(s): 83c82a6

Create app.py

Browse files

https://github.com/mito-ds/mito-for-streamlit-demo/blob/main/main.py

Files changed (1) hide show
  1. app.py +45 -0
app.py ADDED
@@ -0,0 +1,45 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from datetime import datetime, timedelta
2
+
3
+ import pandas as pd
4
+ import streamlit as st
5
+ from mitosheet.streamlit.v1 import spreadsheet
6
+ from mitosheet.streamlit.v1.spreadsheet import _get_mito_backend
7
+
8
+ st.set_page_config(layout="wide")
9
+
10
+ @st.cache_data
11
+ def get_tesla_data():
12
+ df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/tesla-stock-price.csv')
13
+ df = df.drop(0)
14
+ df['volume'] = df['volume'].astype(float)
15
+ return df
16
+
17
+ tesla_data = get_tesla_data()
18
+
19
+ new_dfs, code = spreadsheet(tesla_data)
20
+ code = code if code else "# Edit the spreadsheet above to generate code"
21
+ st.code(code)
22
+
23
+ def clear_mito_backend_cache():
24
+ _get_mito_backend.clear()
25
+
26
+ # Function to cache the last execution time - so we can clear periodically
27
+ @st.cache_resource
28
+ def get_cached_time():
29
+ # Initialize with a dictionary to store the last execution time
30
+ return {"last_executed_time": None}
31
+
32
+ def try_clear_cache():
33
+
34
+ # How often to clear the cache
35
+ CLEAR_DELTA = timedelta(hours=12)
36
+
37
+ current_time = datetime.now()
38
+ cached_time = get_cached_time()
39
+
40
+ # Check if the current time is different from the cached last execution time
41
+ if cached_time["last_executed_time"] is None or cached_time["last_executed_time"] + CLEAR_DELTA < current_time:
42
+ clear_mito_backend_cache()
43
+ cached_time["last_executed_time"] = current_time
44
+
45
+ try_clear_cache()