youngtsai commited on
Commit
53fda43
·
1 Parent(s): d5066a1
Files changed (1) hide show
  1. app.py +156 -20
app.py CHANGED
@@ -1,23 +1,159 @@
1
- from dash import Dash, html
2
- import dash_cytoscape as cyto
3
-
4
- app = Dash(__name__)
5
-
6
- app.layout = html.Div([
7
- html.P("Dash Cytoscape:"),
8
- cyto.Cytoscape(
9
- id='cytoscape',
10
- elements=[
11
- {'data': {'id': 'ca', 'label': 'Canada'}},
12
- {'data': {'id': 'on', 'label': 'Ontario'}},
13
- {'data': {'id': 'qc', 'label': 'Quebec'}},
14
- {'data': {'source': 'ca', 'target': 'on'}},
15
- {'data': {'source': 'ca', 'target': 'qc'}}
16
- ],
17
- layout={'name': 'breadthfirst'},
18
- style={'width': '400px', 'height': '500px'}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19
  )
20
- ])
21
 
 
 
 
 
 
22
 
23
- app.run_server(debug=True)
 
1
+ import networkx as nx
2
+ import matplotlib.pyplot as plt
3
+ import gradio as gr
4
+ import io
5
+ import base64
6
+ import requests
7
+ import json
8
+ import os
9
+
10
+ # Env Vars
11
+ METABASE_USERNAME = os.getenv('METABASE_USERNAME')
12
+ METABASE_PASSWORD = os.getenv('METABASE_PASSWORD')
13
+
14
+ def query_metabase(username, password, class_code, card_id, user_id):
15
+ try:
16
+ # 获取会话令牌
17
+ session_response = requests.post(
18
+ 'https://metabase.cloud.junyiacademy.org/api/session',
19
+ headers={'Content-Type': 'application/json'},
20
+ json={'username': username, 'password': password}
21
+ )
22
+ session_response.raise_for_status()
23
+ session_token = session_response.json()['id']
24
+ print(f"Session token: {session_token}")
25
+
26
+ # 打印请求信息
27
+ request_payload = {
28
+ "parameters": [
29
+ {
30
+ "type": "category",
31
+ "target": ["variable", ["template-tag", "class_code"]],
32
+ "value": class_code
33
+ },
34
+ {
35
+ "type": "category",
36
+ "target": ["variable", ["template-tag", "user_id"]],
37
+ "value": user_id
38
+ }
39
+ ]
40
+ }
41
+ print(f"Request payload: {json.dumps(request_payload, indent=2)}")
42
+
43
+ # 使用提供的 card_id 查询 Metabase 卡片
44
+ query_response = requests.post(
45
+ f'https://metabase.cloud.junyiacademy.org/api/card/{card_id}/query/json',
46
+ headers={
47
+ 'Content-Type': 'application/json',
48
+ 'X-Metabase-Session': session_token
49
+ },
50
+ json=request_payload
51
+ )
52
+
53
+ # 打印响应状态码和内容
54
+ print(f"Response status code: {query_response.status_code}")
55
+ print(f"Response content: {query_response.text}")
56
+
57
+ query_response.raise_for_status()
58
+ return query_response.json()
59
+ except requests.RequestException as e:
60
+ print(f"Failed to query Metabase card: {str(e)}")
61
+ return {"error": f"Failed to query Metabase card: {str(e)}"}
62
+ except Exception as e:
63
+ print(f"Error: {str(e)}")
64
+ return {"error": str(e)}
65
+
66
+ # 创建一个DAG
67
+ def create_knowledge_dag():
68
+ G = nx.DiGraph()
69
+
70
+ # 添加节点
71
+ G.add_node("jnc-4-05-1-1", label="真分數、假分數與帶分數的命名及說、讀、聽、寫、做")
72
+ G.add_node("jnc-4-05-2-1", label="假分數與帶分數的互換")
73
+ G.add_node("jnc-4-05-3-1", label="同分母分數的大小比較")
74
+ G.add_node("jnc-4-05-3-2", label="同分母分數的加減")
75
+ G.add_node("jnc-4-05-3-3", label="分數的整數倍")
76
+ G.add_node("jnc-4-06-1-1", label="認識等值分數")
77
+ G.add_node("jnc-4-06-1-2", label="找出等值分數")
78
+ G.add_node("jnc-4-06-2-1", label="簡單異分母分數的比較")
79
+ G.add_node("jnc-4-06-2-2", label="簡單異分母分數的加減")
80
+ G.add_node("jnc-4-06-3-1", label="分數與一位小數的互換")
81
+ G.add_node("jnc-4-06-3-2", label="分數與二位小數的互換")
82
+ G.add_node("jnc-4-08-2-1", label="認識分數數線")
83
+ G.add_node("jnc-4-08-2-2", label="數線的整數、分數、小數")
84
+
85
+ # 添加边
86
+ G.add_edge("jnc-4-05-1-1", "jnc-4-05-2-1")
87
+ G.add_edge("jnc-4-05-1-1", "jnc-4-05-3-1")
88
+ G.add_edge("jnc-4-05-2-1", "jnc-4-05-3-1")
89
+ G.add_edge("jnc-4-05-1-1", "jnc-4-05-3-2")
90
+ G.add_edge("jnc-4-05-2-1", "jnc-4-05-3-2")
91
+ G.add_edge("jnc-4-05-2-1", "jnc-4-05-3-3")
92
+ G.add_edge("jnc-4-05-3-2", "jnc-4-05-3-3")
93
+ G.add_edge("jnc-4-05-1-1", "jnc-4-06-1-1")
94
+ G.add_edge("jnc-4-06-1-1", "jnc-4-06-1-2")
95
+ G.add_edge("jnc-4-06-1-1", "jnc-4-06-2-1")
96
+ G.add_edge("jnc-4-06-1-2", "jnc-4-06-2-1")
97
+ G.add_edge("jnc-4-06-1-1", "jnc-4-06-2-2")
98
+ G.add_edge("jnc-4-06-1-2", "jnc-4-06-2-2")
99
+ G.add_edge("jnc-4-06-3-1", "jnc-4-06-3-2")
100
+ G.add_edge("jnc-4-05-1-1", "jnc-4-08-2-1")
101
+ G.add_edge("jnc-4-08-2-1", "jnc-4-08-2-2")
102
+ G.add_edge("jnc-4-05-1-1", "jnc-4-06-3-1")
103
+
104
+ return G
105
+
106
+ # 绘制DAG并返回图像数据
107
+ def plot_knowledge_dag():
108
+ G = create_knowledge_dag()
109
+
110
+ plt.figure(figsize=(10, 8))
111
+ pos = nx.spring_layout(G)
112
+ nx.draw(G, pos, with_labels=True, node_color='skyblue', node_size=2000, edge_color='gray', linewidths=2, font_size=10, font_weight='bold')
113
+ labels = nx.get_node_attributes(G, 'label')
114
+ nx.draw_networkx_labels(G, pos, labels, font_size=8, font_color='black')
115
+
116
+ buf = io.BytesIO()
117
+ plt.savefig(buf, format='png')
118
+ buf.seek(0)
119
+
120
+ return base64.b64encode(buf.read()).decode('utf-8')
121
+
122
+ # 定义Gradio接口
123
+ def show_knowledge_dag(result):
124
+ if 'error' in result:
125
+ return result['error']
126
+
127
+ image_data = plot_knowledge_dag()
128
+ return f'<img src="data:image/png;base64,{image_data}"/>'
129
+
130
+ with gr.Blocks() as app:
131
+ gr.Markdown("# Metabase Query and Visualization")
132
+
133
+ with gr.Row():
134
+ username = gr.Textbox(label="Metabase Username", value="purpleice9765@msn.com")
135
+ password = gr.Textbox(label="Metabase Password", type="password", value=METABASE_PASSWORD)
136
+
137
+ with gr.Row():
138
+ class_code = gr.Textbox(label="Class Code", value="class2")
139
+ card_id = gr.Textbox(label="Card ID", value="6267")
140
+ user_id = gr.Textbox(label="User ID", value="stu26")
141
+
142
+ result = gr.JSON(label="Query Result")
143
+ graph_html = gr.HTML()
144
+
145
+ query_button = gr.Button("Fetch Data")
146
+
147
+ query_button.click(
148
+ fn=query_metabase,
149
+ inputs=[username, password, class_code, card_id, user_id],
150
+ outputs=result
151
  )
 
152
 
153
+ result.change(
154
+ fn=show_knowledge_dag,
155
+ inputs=result,
156
+ outputs=graph_html
157
+ )
158
 
159
+ app.launch()