Spaces:
Sleeping
Sleeping
import gradio as gr | |
import rpy2.robjects as robj | |
from rpy2.robjects import default_converter | |
from rpy2.robjects.conversion import localconverter | |
# define R functions globally | |
robj.r(""" | |
add <- function(x,y) { return(x + y) } | |
multiply <- function(x,y) { return(x * y) } | |
sub <- function(x,y) { return(x - y) } | |
div <- function(x,y) { return(x / y) } | |
""") | |
def arithmetic(num1, num2, operation): | |
with localconverter(default_converter): | |
if operation == "Add": | |
res = robj.r(f"add({num1}, {num2})") | |
elif operation == "Multiply": | |
res = robj.r(f"multiply({num1}, {num2})") | |
elif operation == "Subtract": | |
res = robj.r(f"sub({num1}, {num2})") | |
elif operation == "Divide": | |
res = robj.r(f"div({num1}, {num2})") | |
return res[0] | |
custom_css = """ | |
/* Set the overall background to a dark color */ | |
.gradio-container { | |
background-color: #121212; | |
color: #ffffff; | |
} | |
/* Style the title and description text */ | |
.gradio-container h1, .gradio-container h2, .gradio-container p { | |
color: #ffffff; | |
} | |
/* Style the input fields with a dark background and blue border */ | |
input, textarea { | |
background-color: #1e1e1e; | |
border: 2px solid #2a5298; | |
color: #ffffff; | |
padding: 10px; | |
border-radius: 5px; | |
width: 100%; | |
box-sizing: border-box; | |
} | |
/* Change border color on focus */ | |
input:focus, textarea:focus { | |
border-color: #1e3c72; | |
outline: none; | |
} | |
/* Style the buttons with a gradient blue background */ | |
button { | |
background: linear-gradient(to right, #1e3c72, #2a5298); | |
color: #ffffff; | |
border: none; | |
padding: 10px 20px; | |
border-radius: 5px; | |
cursor: pointer; | |
transition: background 0.3s ease; | |
} | |
/* Change button background on hover */ | |
button:hover { | |
background: linear-gradient(to right, #2a5298, #1e3c72); | |
} | |
/* Style the labels */ | |
label { | |
color: #ffffff; | |
font-weight: bold; | |
margin-top: 10px; | |
display: block; | |
} | |
/* Hide footer display */ | |
footer {display: none !important;} | |
""" | |
with gr.Blocks( | |
title="R-Powered Calculator", | |
theme=gr.themes.Base(), | |
css = custom_css | |
) as app: | |
gr.HTML( | |
""" | |
<h1 style='text-align: center'> | |
<strong>R-Powered Calculator</strong> | |
</h1> | |
""" | |
) | |
with gr.Row(): | |
num1 = gr.Number(label="Number 1") | |
num2 = gr.Number(label="Number 2") | |
with gr.Column(): | |
operation = gr.Radio(choices=["Add", "Multiply", "Subtract", "Divide"], | |
value="Add", | |
label="Choose Arithmetic Operation", | |
min_width=100, | |
scale=0.3) | |
with gr.Row(): | |
submit = gr.Button("Run Operation") | |
with gr.Column(): | |
gr.HTML( | |
""" | |
<h3 style='text-align: center'> | |
<strong>Arithmetic Operation Result</strong> | |
</h3> | |
""" | |
) | |
result = gr.Number() | |
submit.click(arithmetic, inputs=[num1, num2, operation], outputs=[result]) | |
app.launch(server_name="0.0.0.0", server_port=7860) | |