= [
questions
{"question": "What is the capital of France?",
"choices": ["Paris", "Rome", "Berlin", "Madrid"],
"answer": "Paris"
},
{"question": "Which planet is closest to the sun?",
"choices": ["Venus", "Earth", "Mercury", "Mars"],
"answer": "Mercury"
},
{"question": "What is the largest mammal?",
"choices": ["Elephant", "Blue Whale", "Giraffe", "Tiger"],
"answer": "Blue Whale"
} ]
#| '!! shinylive warning !!': |
#| shinylive does not work in self-contained HTML documents.
#| Please set `embed-resources: false` in your metadata.
#| standalone: true
import shiny
from shiny import *
app = shiny.App()
@app.ui
def ui():
return shiny.page_fluid(
shiny.h2("Quiz App"),
shiny.ui_select_input("question", "Select a question:",
choices=[q['question'] for q in questions]),
shiny.ui_radio_buttons("answer", "Choose an answer:", choices=[]),
shiny.ui_button("submit", "Submit Answer"),
shiny.ui_output_text_verbatim("feedback")
)
@app.server
def server(input: Inputs, output: Outputs, session: Session):
@shiny.reactive.Effect
def update_choices():
selected_question = input.question()
if selected_question:
for q in questions:
if q["question"] == selected_question:
session.update_radio_buttons("answer", choices=q["choices"])
break
@output
@shiny.render.text
def feedback():
if input.answer() and input.submit():
selected_question = input.question()
selected_answer = input.answer()
correct_answer = next(q["answer"] for q in questions if q["question"] == selected_question)
if selected_answer == correct_answer:
return "Correct!"
else:
return f"Incorrect! The correct answer was: {correct_answer}"
if __name__ == "__main__":
app.run()
Reuse
CC SA BY-NC-ND
Citation
BibTeX citation:
@online{bochman2024,
author = {Bochman, Oren},
title = {Quiz {App} with {ShinyLive}},
date = {2024-12-20},
url = {https://orenbochman.github.io/notes/bayesian-ts/my-quizzez/quizz.html},
langid = {en}
}
For attribution, please cite this work as:
Bochman, Oren. 2024. “Quiz App with ShinyLive.” December
20, 2024. https://orenbochman.github.io/notes/bayesian-ts/my-quizzez/quizz.html.