opensafely-core / ehrql

ehrQL: the electronic health record query language for OpenSAFELY
https://docs.opensafely.org/ehrql/
Other
7 stars 3 forks source link

Investigate adding a quiz mechanism to the sandbox #2168

Open inglesp opened 1 week ago

inglesp commented 1 week ago

A good way of giving new users confidence in their ability to write ehrQL is to give them a quiz that they can use to test their understanding. It would be good if doing the quiz felt as close to writing real ehrQL as possible, and this should be possible through the sandbox.

For instance:

>>> from ehrql import quiz
>>> quiz.start()
Welcome to the ehrQL quiz!

[instructions go here...]

Q1: filter the clinical_events frame so that it only contains records with the code XXX.

>>> from ehrql.tables.core import clinical_events  # maybe this should be auto-imported?
>>> e = clinical_events.where(clinical_events.code == XXX)
>>> quiz.check(e)
Correct

Q2: filter the frame further so that it only contains events that happened on or after 1st January 2023.

>>> e = e.where(e.date.is_on_or_after("2022-01-01"))
>>> quiz.check(e)
Incorrect
Your answer contains 1 unexpected row:

[diff goes here...]

This ticket does not include coming up with sensible quiz questions, but instead will require developing (a) a structure for storing questions and answers and (b) a module (ehrql.quiz) for interacting with them, perhaps with the functions start and check as shown above.

It might also make sense to change how the sandbox command works, so that if it's not provided with any arguments then it uses example data that ships with ehrQL.