Update space
Browse files
app.py
CHANGED
@@ -53,30 +53,50 @@ def extract_table(url):
|
|
53 |
if not table:
|
54 |
return "<p>No table found on page</p>"
|
55 |
|
|
|
56 |
data = []
|
57 |
rows = table.find_all('tr')
|
58 |
for i, row in enumerate(rows[1:]):
|
59 |
cells = row.find_all('td')
|
60 |
if len(cells) >= 2:
|
61 |
data.append({
|
|
|
62 |
'Date': cells[0].text.strip()[:10],
|
63 |
'Topic': cells[1].text.strip(),
|
64 |
})
|
65 |
|
66 |
-
|
67 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
68 |
except Exception as e:
|
69 |
return f"<p>Error: {str(e)}</p>"
|
70 |
|
71 |
|
|
|
72 |
def display_table(url):
|
73 |
return extract_table(url)
|
74 |
|
75 |
-
def
|
76 |
-
|
77 |
-
|
78 |
-
return message
|
79 |
-
|
80 |
|
81 |
"""
|
82 |
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
@@ -98,7 +118,6 @@ with gr.Blocks() as demo:
|
|
98 |
)
|
99 |
|
100 |
with gr.Column(scale=2):
|
101 |
-
|
102 |
chatbot = gr.ChatInterface(
|
103 |
respond,
|
104 |
additional_inputs=[
|
@@ -106,6 +125,32 @@ with gr.Blocks() as demo:
|
|
106 |
],
|
107 |
)
|
108 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
109 |
|
110 |
if __name__ == "__main__":
|
111 |
demo.launch()
|
|
|
53 |
if not table:
|
54 |
return "<p>No table found on page</p>"
|
55 |
|
56 |
+
# Extract data
|
57 |
data = []
|
58 |
rows = table.find_all('tr')
|
59 |
for i, row in enumerate(rows[1:]):
|
60 |
cells = row.find_all('td')
|
61 |
if len(cells) >= 2:
|
62 |
data.append({
|
63 |
+
'Index': i,
|
64 |
'Date': cells[0].text.strip()[:10],
|
65 |
'Topic': cells[1].text.strip(),
|
66 |
})
|
67 |
|
68 |
+
# Generate HTML table with buttons
|
69 |
+
html_rows = ""
|
70 |
+
for row in data:
|
71 |
+
html_rows += f"""
|
72 |
+
<tr>
|
73 |
+
<td>{row['Date']}</td>
|
74 |
+
<td>{row['Topic']}</td>
|
75 |
+
<td><button onclick="handlePrepare({row['Index']})">Prepare</button></td>
|
76 |
+
</tr>
|
77 |
+
"""
|
78 |
+
html_table = f"""
|
79 |
+
<table border="1">
|
80 |
+
<tr>
|
81 |
+
<th>Date</th>
|
82 |
+
<th>Topic</th>
|
83 |
+
<th>Action</th>
|
84 |
+
</tr>
|
85 |
+
{html_rows}
|
86 |
+
</table>
|
87 |
+
"""
|
88 |
+
return html_table
|
89 |
except Exception as e:
|
90 |
return f"<p>Error: {str(e)}</p>"
|
91 |
|
92 |
|
93 |
+
|
94 |
def display_table(url):
|
95 |
return extract_table(url)
|
96 |
|
97 |
+
def handle_prepare(index):
|
98 |
+
topic = data[index]["Topic"]
|
99 |
+
return f"Prepare a 10-minute reading on what I should know before the class for the topic: {topic}"
|
|
|
|
|
100 |
|
101 |
"""
|
102 |
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
|
|
118 |
)
|
119 |
|
120 |
with gr.Column(scale=2):
|
|
|
121 |
chatbot = gr.ChatInterface(
|
122 |
respond,
|
123 |
additional_inputs=[
|
|
|
125 |
],
|
126 |
)
|
127 |
|
128 |
+
# Dynamically generate buttons for "Prepare" actions
|
129 |
+
prepare_buttons = gr.Column(visible=False)
|
130 |
+
|
131 |
+
def create_prepare_buttons():
|
132 |
+
# Create individual buttons for each topic
|
133 |
+
components = []
|
134 |
+
for i, row in enumerate(data):
|
135 |
+
btn = gr.Button(f"Prepare: {row['Topic']}")
|
136 |
+
btn.click(
|
137 |
+
fn=handle_prepare,
|
138 |
+
inputs=[gr.State(i)], # Pass the index of the row
|
139 |
+
outputs=[],
|
140 |
+
).then(
|
141 |
+
fn=lambda message: ([message],), # Feed to chatbot
|
142 |
+
inputs=[],
|
143 |
+
outputs=[chatbot],
|
144 |
+
)
|
145 |
+
components.append(btn)
|
146 |
+
return components
|
147 |
+
|
148 |
+
extract_btn.click(
|
149 |
+
fn=create_prepare_buttons,
|
150 |
+
inputs=[],
|
151 |
+
outputs=[prepare_buttons],
|
152 |
+
)
|
153 |
+
|
154 |
|
155 |
if __name__ == "__main__":
|
156 |
demo.launch()
|