Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| def calculator(input_text): | |
| try: | |
| return str(eval(input_text)) | |
| except: | |
| return "Error" | |
| def update_input(input_text, new_value): | |
| if new_value == "C": | |
| return "" | |
| elif new_value == "=": | |
| try: | |
| return str(eval(input_text)) | |
| except: | |
| return "Error" | |
| else: | |
| return input_text + new_value | |
| with gr.Blocks() as iface: | |
| gr.Markdown("# Simple Calculator") | |
| with gr.Row(): | |
| input_display = gr.Textbox(value="", label="Display") | |
| button_layout = [ | |
| ["7", "8", "9", "/"], | |
| ["4", "5", "6", "*"], | |
| ["1", "2", "3", "-"], | |
| ["0", ".", "C", "+"], | |
| ["="] | |
| ] | |
| for row in button_layout: | |
| with gr.Row(): | |
| for button in row: | |
| btn = gr.Button(button) | |
| btn.click(update_input, inputs=[input_display, btn], outputs=input_display) | |
| iface.launch() |