File size: 1,938 Bytes
efa0177
 
bad3ba3
efa0177
3c44397
 
 
 
 
 
efa0177
3c44397
 
 
 
bad3ba3
3c44397
 
 
 
 
 
 
 
 
 
 
68ba491
 
 
3c44397
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
68ba491
bad3ba3
3c44397
efa0177
3c44397
 
 
 
 
bad3ba3
3c44397
 
bad3ba3
3c44397
 
efa0177
bad3ba3
3c44397
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import gradio as gr
import pandas as pd
import json

# 读取Excel文件
def read_excel(file_path):
    df = pd.read_excel(file_path)
    # 提取第一列的数据
    first_column_data = df.iloc[:, 0].tolist()
    return first_column_data

# 生成问卷调查界面
def generate_survey(data):
    # 将数据转换为Markdown格式
    markdown_text = "\n".join([f"- {item}" for item in data])
    
    # 创建Gradio界面
    with gr.Blocks() as demo:
        gr.Markdown("## 问卷调查")
        gr.Markdown(markdown_text)
        
        # 创建一个文本输入框用于打分
        score = gr.Textbox(label="请打分 (1-5)", placeholder="输入1到5之间的数字")
        
        # 创建一个提交按钮
        submit_button = gr.Button("提交")
        
        # 创建一个输出文本框
        output_text = gr.Textbox(label="提交结果", interactive=False)
        
        # 定义一个回调函数来处理提交
        def submit(score):
            try:
                score = int(score)
                if 1 <= score <= 5:
                    # 将结果保存到JSON文件
                    result = {"score": score}
                    with open("survey_result.json", "w") as f:
                        json.dump(result, f)
                    return "提交成功!"
                else:
                    return "请输入1到5之间的数字。"
            except ValueError:
                return "请输入有效的数字。"
        
        # 绑定提交按钮的回调函数
        submit_button.click(submit, inputs=score, outputs=output_text)
    
    return demo

# 主函数
def main():
    # 读取Excel文件
    file_path = "survey_data.xlsx"  # 替换为你的Excel文件路径
    data = read_excel(file_path)
    
    # 生成问卷调查界面
    demo = generate_survey(data)
    
    # 启动Gradio应用
    demo.launch()

if __name__ == "__main__":
    main()