Spaces:
Sleeping
Sleeping
Create components/notebook_ui.py
Browse files- components/notebook_ui.py +61 -0
components/notebook_ui.py
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# components/notebook_ui.py
|
| 2 |
+
import streamlit as st
|
| 3 |
+
from typing import Dict, List, Optional
|
| 4 |
+
import json
|
| 5 |
+
|
| 6 |
+
class NotebookUI:
|
| 7 |
+
def __init__(self):
|
| 8 |
+
if 'notebook_cells' not in st.session_state:
|
| 9 |
+
st.session_state.notebook_cells = []
|
| 10 |
+
|
| 11 |
+
def add_cell(self, cell_type: str, content: Dict, metadata: Optional[Dict] = None):
|
| 12 |
+
"""Add a new cell to the notebook"""
|
| 13 |
+
cell = {
|
| 14 |
+
'type': cell_type,
|
| 15 |
+
'content': content,
|
| 16 |
+
'metadata': metadata or {},
|
| 17 |
+
'id': len(st.session_state.notebook_cells)
|
| 18 |
+
}
|
| 19 |
+
st.session_state.notebook_cells.append(cell)
|
| 20 |
+
|
| 21 |
+
def render_cells(self):
|
| 22 |
+
"""Render all notebook cells"""
|
| 23 |
+
for cell in st.session_state.notebook_cells:
|
| 24 |
+
self._render_cell(cell)
|
| 25 |
+
|
| 26 |
+
def _render_cell(self, cell: Dict):
|
| 27 |
+
"""Render a single cell"""
|
| 28 |
+
with st.container():
|
| 29 |
+
with st.expander(f"Cell {cell['id'] + 1}", expanded=True):
|
| 30 |
+
if cell['type'] == 'query':
|
| 31 |
+
self._render_query_cell(cell)
|
| 32 |
+
elif cell['type'] == 'analysis':
|
| 33 |
+
self._render_analysis_cell(cell)
|
| 34 |
+
elif cell['type'] == 'document':
|
| 35 |
+
self._render_document_cell(cell)
|
| 36 |
+
|
| 37 |
+
def _render_query_cell(self, cell: Dict):
|
| 38 |
+
st.markdown("**Query:**")
|
| 39 |
+
st.write(cell['content']['query'])
|
| 40 |
+
st.markdown("**Response:**")
|
| 41 |
+
st.write(cell['content']['response'])
|
| 42 |
+
|
| 43 |
+
col1, col2 = st.columns([1, 4])
|
| 44 |
+
with col1:
|
| 45 |
+
if st.button("♻️ Regenerate", key=f"regen_{cell['id']}"):
|
| 46 |
+
pass # Implement regeneration logic
|
| 47 |
+
if st.button("📋 Copy", key=f"copy_{cell['id']}"):
|
| 48 |
+
st.toast("Copied to clipboard!")
|
| 49 |
+
|
| 50 |
+
def _render_analysis_cell(self, cell: Dict):
|
| 51 |
+
st.markdown(f"**{cell['metadata'].get('template', 'Analysis')}**")
|
| 52 |
+
for section in cell['content']:
|
| 53 |
+
st.markdown(f"### {section['title']}")
|
| 54 |
+
st.write(section['content'])
|
| 55 |
+
|
| 56 |
+
def _render_document_cell(self, cell: Dict):
|
| 57 |
+
st.markdown(f"**Document: {cell['metadata'].get('filename', 'Unknown')}**")
|
| 58 |
+
with st.expander("Document Content"):
|
| 59 |
+
st.write(cell['content']['text'])
|
| 60 |
+
st.markdown("### Metadata")
|
| 61 |
+
st.json(cell['metadata'])
|