letxinet / modules /history_tab.py
C2MV's picture
Initial upload for Build Small Hackathon
68fb5e2 verified
Raw
History Blame Contribute Delete
1.56 kB
import gradio as gr
from backend.database.models import SessionLocal, User, Project, ResearchJob
def load_history():
db = SessionLocal()
user = db.query(User).filter(User.username == "admin").first()
if not user:
db.close()
return [["", "Usuario no encontrado."]]
jobs = db.query(ResearchJob).join(Project).filter(Project.owner_id == user.id).order_by(ResearchJob.created_at.desc()).all()
history_data = []
for job in jobs:
history_data.append([
job.project.title,
job.query,
job.status,
job.created_at.strftime("%Y-%m-%d %H:%M:%S")
])
db.close()
if not history_data:
return [["-", "No hay investigaciones guardadas.", "-", "-"]]
return history_data
def create_history_tab():
with gr.Tab("🕒 Historial", id="history"):
gr.Markdown("## Historial de Investigaciones")
gr.Markdown("Aquí se guardan las investigaciones de tu cuenta.")
refresh_btn = gr.Button("🔄 Actualizar Historial")
history_table = gr.Dataframe(
headers=["Proyecto", "Consulta", "Estado", "Fecha"],
interactive=False,
wrap=True
)
refresh_btn.click(fn=load_history, inputs=None, outputs=[history_table])
# Load automatically when switching tabs isn't natively supported in Blocks without an event.
# But we can bind it to the app load event, which we'll do in app.py or just let the user click refresh.