Manipulation / calculator.py
Swathi6's picture
Create calculator.py
dad3afe verified
raw
history blame
1.11 kB
import gradio as gr
# Function to perform the calculator operations
def calculate_expression(expression):
try:
# Using eval to evaluate the mathematical expression
result = eval(expression)
return result
except:
return "Error"
# Create the interface using Gradio
def create_calculator():
with gr.Blocks() as demo:
gr.Markdown("### Simple Calculator")
# Create the input box where users can enter expressions
expression_input = gr.Textbox(label="Enter Expression", placeholder="e.g., 3 + 4", lines=1)
# Output area to show the result
result_output = gr.Textbox(label="Result", interactive=False)
# Button to trigger calculation
calculate_button = gr.Button("Calculate")
# Link the button to the calculation function
calculate_button.click(fn=calculate_expression, inputs=expression_input, outputs=result_output)
return demo
# Run the Gradio app
if __name__ == "__main__":
calculator_app = create_calculator()
calculator_app.launch()