RubyHu/Test-Calculator
0
1import gradio as gr2 3def calculator(num1, operation, num2):4 if operation == "+":5 return num1 + num26 elif operation == "-":7 return num1 - num28 elif operation == "x":9 return num1 * num210 elif operation == "/":11 if num2 == 0:12 raise gr.Error("Cannot divide by zero!")13 return num1 / num214 15examples=[16 [5, "+", 3],17 [4, "-", 2],18 [-4, "x", 2.5],19 [0, "/", 1.2],20]21 22with gr.Blocks(theme='freddyaboulton/dracula_revamped') as demo:23 with gr.Row():24 gr.Markdown('''25 <h1 style="text-align:center">計算機</h1>26 <p>這是一個計算機示例。允許您計算如 2+2=4 等式子。 </p>27 ''')28 with gr.Row():29 #輸入30 n1=gr.Number(label="數字1")31 oper=gr.Radio(["+", "-", "x", "/"],label="算數")32 n2=gr.Number(label="數字2")33 with gr.Row():34 #設定按鈕35 calculate_btn = gr.Button("計算!", variant="primary")36 with gr.Row():37 #輸出38 out=gr.Number(label="答案")39 with gr.Row():40 clear = gr.ClearButton([n1, oper,n2, out], value="清除")41 with gr.Row():42 gr.Examples(examples, [n1, oper,n2])43 44 #設定按鈕點選事件45 calculate_btn.click(fn=calculator, inputs=[n1, oper,n2], outputs=out)46 47demo.launch()