import random
import ipywidgets as widgets
from IPython.display import display, clear_output
# Define the function to get a random rating and comment
def get_medication_rating(medication):
rating = random.randint(1, 5)
comments = {
1: "It sucks",
2: "Could be better, got a reaction from it",
3: "It's okay",
4: "This is great, I love it",
5: "Flawless"
}
comment = comments[rating]
return f"{medication} has a rating of {rating} out of 5 stars. {comment}"
# Define the function to handle the button click
def on_button_click(button):
clear_output(wait=True)
medication = text.value
result = get_medication_rating(medication)
display(result, text, button)
# Create the text input and button widgets
text = widgets.Text(
value='',
placeholder='Enter the name of the medication',
description='Medication:',
disabled=False
)
button = widgets.Button(description="Get Rating")
button.on_click(on_button_click)
# Display the widgets
display(text, button)
'Advil has a rating of 2 out of 5 stars. Could be better, got a reaction from it'
Text(value='Advil', description='Medication:', placeholder='Enter the name of the medication')
Button(description='Get Rating', style=ButtonStyle())