From fadc1e5c47472541d85a127c291c8a20afaaa8a4 Mon Sep 17 00:00:00 2001 From: ethanfel Date: Fri, 2 Jan 2026 13:13:37 +0100 Subject: [PATCH] Create tab_timeline.py --- tab_timeline.py | 111 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 111 insertions(+) create mode 100644 tab_timeline.py diff --git a/tab_timeline.py b/tab_timeline.py new file mode 100644 index 0000000..5a8087a --- /dev/null +++ b/tab_timeline.py @@ -0,0 +1,111 @@ +import streamlit as st +import json +import graphviz +from history_tree import HistoryTree +from utils import save_json + +def render_timeline_tab(data, file_path): + # 1. Initialize Tree + tree_data = data.get("history_tree", {}) + if not tree_data: + st.info("No history timeline exists for this file yet. Make some changes in the Editor first!") + return + + htree = HistoryTree(tree_data) + + # 2. Horizontal Visualizer (Fusion 360 Style) + st.subheader("🕰️ Version History") + try: + graph_dot = htree.generate_horizontal_graph() + st.graphviz_chart(graph_dot, use_container_width=True) + except Exception as e: + st.error(f"Graph Error (Ensure 'graphviz' is installed in Docker): {e}") + + st.markdown("---") + + # 3. The "Scrubber" / Inspector + # Since we can't click the graph directly easily, we use a selectbox as our "Time Scrubber" + col_sel, col_act = st.columns([3, 1]) + + all_nodes = list(htree.nodes.values()) + all_nodes.sort(key=lambda x: x["timestamp"], reverse=True) # Newest first + + # Format: "Note (ID) - [Branch]" + def fmt_node(n): + return f"{n.get('note', 'Step')} ({n['id']})" + + with col_sel: + # Default to current HEAD + current_idx = 0 + for i, n in enumerate(all_nodes): + if n["id"] == htree.head_id: + current_idx = i + break + + selected_node = st.selectbox( + "Inspect Node (Select to View/Edit):", + all_nodes, + format_func=fmt_node, + index=current_idx + ) + + # 4. The Editor (Inspector Panel) + if selected_node: + node_data = selected_node["data"] + + st.info(f"Viewing State: **{selected_node.get('note')}** (ID: {selected_node['id']})") + + with st.expander("📝 View / Edit Data for this Point", expanded=True): + # We show raw JSON here for total control since it's an advanced tab + # But you can edit specific keys if you want to be granular + + # Editable JSON Text Area + edited_json_str = st.text_area( + "Raw Data (JSON)", + value=json.dumps(node_data, indent=4), + height=300 + ) + + # 5. Restore / Branch Button + with col_act: + st.write("") # Spacer + st.write("") + if st.button("⏪ Restore & Branch", type="primary", use_container_width=True): + try: + new_data_content = json.loads(edited_json_str) + + # 1. Update the Main Data + data.update(new_data_content) + + # 2. Update the Tree (Move HEAD to this node) + # If the user EDITED the JSON, we should probably commit a NEW node automatically + # But if they just clicked restore, we move HEAD. + + # Logic: Just move head to selected, then saving in future will branch. + htree.head_id = selected_node['id'] + + # 3. Save to Disk + data["history_tree"] = htree.to_dict() + save_json(file_path, data) + + # 4. Reset UI + st.session_state.ui_reset_token += 1 + st.toast(f"Restored to {selected_node['id']}!", icon="↺") + st.rerun() + + except json.JSONDecodeError: + st.error("Invalid JSON format in editor.") + + # 6. Delete Option + with st.expander("Danger Zone"): + if st.button("🗑️ Delete this History Node"): + if selected_node['id'] in htree.nodes: + del htree.nodes[selected_node['id']] + # Fix branches pointing to this + for b, tip in list(htree.branches.items()): + if tip == selected_node['id']: + del htree.branches[b] # Prune broken branch tip + + data["history_tree"] = htree.to_dict() + save_json(file_path, data) + st.rerun()