Making Interactive Quizzes with Gradio in Google Colab
A simple way to make your technical workshops more engaging.
Whenever I conduct workshops, I like to end them with quizzes. It’s a fun and engaging way to review what we covered and a great way to recap key points.
I often use Google Colab for my workshops and initially used Colab Forms for quizzes. The main drawback was that the answers were visible in the notebook, allowing attendees to peek. After trying external quiz tools for a while, I found a much simpler and more secure solution via Gradio.
Gradio is a versatile, open-source Python library for building interactive web interfaces with pure Python, which I frequently use for my machine learning projects. The best part? Gradio runs directly inside Colab, so I can create quiz interfaces right in the notebook. Attendees can type their answers, get an instant score, and, if they pass, download a personalized PDF certificate that I’ve built into the workflow. The good news is that now Gradio comes preinstalled in Google Colab, and so there is no need to even install it in the notebook environment.
In this short article, I’ll show you my quiz setup and how you can replicate it for your own workshops or tutorials too.
Creating the Quiz
I created a Quiz Generator app, which is currently hosted on Hugging Face Spaces.
Here is the workflow in simple steps:
- Choose a Quiz Type: Go to the Quiz Generator app, fill in the quiz title and instructor name and select either multiple-choice (MCQ) or text-based answers.
- Enter Questions & Answers: Paste your content, with each new question on a new line, using one of the following formats:
- MCQ Format:Question,Option1,Option2,Option3,CorrectOption- Text Answer Format:Question,CorrectAnswer - Generate Python Code: The app instantly converts your questions into the necessary code for a participant-facing Gradio interface.
- Launch in Colab: Copy the code into a Colab cell. When participants run it, the interactive quiz appears directly in the notebook.
Answer Hashing
An important feature of this quiz generator is that the answers are stored as SHA-256 hashes, so even if participants look at the notebook code, they can’t see the real solutions.
Added Bonus: Automated Certificates
Since we’re already using Gradio, it’s easy to add a feature for generating certificates. An automatic PDF generator powered by the reportlab library can issue certificates instantly to successful participants. This way, as an instructor, you don’t have to email certificates personally. You can of course, style the certificates based on your choice with a minimal change in code.
While this requirement is already covered in the generated code but if you encounter any error, re-install the library as follows:
pip intall reportlabConclusion
I have been using these quizzes for a while now, and they improve the sessions. Attendees stay more engaged because they know there will be a quiz at the end, and they get their results right away. The fact that everything is within the same notebook environment makes the experience seamless.
