import json import subprocess import pandas as pd import streamlit as st from glob import glob mode = st.sidebar.radio("", ["All questions", "Inspect"]) def read_question(file): with open(file, "r") as f: data = json.load(f) return data["question"] data = pd.DataFrame() data["file"] = glob("data/*/*.json") data["category"] = data["file"].apply(lambda x: " ".join(x.split("/")[1].split("_")).title()) data["question"] = data["file"].apply(read_question) if mode == "All questions": grouped_data = data.groupby("category").agg(list).reset_index() for i, row in grouped_data.iterrows(): st.write(f"## {row['category']}") for i, question in enumerate(row["question"]): st.write(f"{i+1}. {question} ({row['file'][i]})") elif mode == "Inspect": category = st.selectbox("Category", data["category"].unique()) question = st.selectbox("Question", data[data["category"] == category]["question"]) row = data[(data["category"] == category) & (data["question"] == question)].iloc[0] with open(row["file"], "r") as f: row_data = json.load(f) with open(row["file"].replace(".json", ".py"), "r") as f: row_code = f.read() st.write( f"""## Code ```python {row_code} ``` """ ) execute = st.button("Execute") if execute: with st.status("", expanded=True): output = subprocess.check_output(["python3", row["file"].replace(".json", ".py")]) print(output) st.write(f"{output.decode('utf-8').strip()}")