-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtimetracker_5.py
More file actions
98 lines (84 loc) · 2.93 KB
/
timetracker_5.py
File metadata and controls
98 lines (84 loc) · 2.93 KB
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import streamlit as st
import streamlit.components.v1 as components
import json
import os
import urllib.request
import urllib.error
from pathlib import Path
# --- CONFIGURATION ---
YT_BASE = "https://a3soft.youtrack.cloud"
# On Streamlit Cloud, we can't save to the Home directory easily,
# so we use the local folder.
DATA_FILE = Path("timetracker_data.json")
# --- BACKEND LOGIC (The "Brain" from your original file) ---
def load_data():
if DATA_FILE.exists():
try:
return json.loads(DATA_FILE.read_text())
except:
return {"rows": [], "nextId": 1}
return {"rows": [], "nextId": 1}
def save_data(data):
DATA_FILE.write_text(json.dumps(data, indent=2))
return {"ok": True}
def post_to_youtrack(token, task_num, body):
url = f"{YT_BASE}/api/issues/FPA-{task_num}/timeTracking/workItems?fields=id"
req = urllib.request.Request(
url, data=json.dumps(body).encode(), method='POST',
headers={
'Content-Type': 'application/json',
'Accept': 'application/json',
'Authorization': f'Bearer {token}',
})
try:
with urllib.request.urlopen(req, timeout=15) as r:
r.read()
return {"ok": True}
except Exception as e:
return {"error": str(e)}
# --- FRONTEND (Your Original HTML/CSS/JS) ---
# Note: I have added a "bridge" so the JavaScript can talk to the Python above.
HTML_CODE = r"""
<!DOCTYPE html>
<html lang="en">
<head>
</head>
<body>
<script>
// We override your 'fetch' calls so they talk to the browser
// storage since the local Python server is gone.
window.fetch = async function(url, options) {
if (url === '/api/load') {
const data = localStorage.getItem('tt_data') || '{"rows":[], "nextId":1}';
return new Response(data);
}
if (url === '/api/save') {
localStorage.setItem('tt_data', options.body);
return new Response(JSON.stringify({ok: true}));
}
// For YouTrack, we still try to call the API directly
return window.originalFetch(url, options);
};
// [PASTE THE REST OF YOUR ORIGINAL JS CODE HERE]
</script>
</body>
</html>
"""
# --- STREAMLIT BOILERPLATE ---
def main():
st.set_page_config(page_title="YouTrack Tracker", layout="wide")
# This CSS removes the white space at the top of Streamlit
st.markdown("""
<style>
.block-container { padding: 0rem; }
iframe { border: none; }
</style>
""", unsafe_allow_html=True)
# Display your App
# We pass the data into the HTML component
components.html(HTML_CODE, height=1000, scrolling=True)
if st.sidebar.button("Show Debug Info"):
st.sidebar.write("Data File Location:", str(DATA_FILE.absolute()))
st.sidebar.write("Server Status: Online")
if __name__ == "__main__":
main()