All posts

ยท Antonio Godoy Pellini

How to run Python in Moodle: notebook cells in a course

Your options for teaching Python with Moodle, and how DTeach puts runnable Python cells inside a course students open from a Moodle activity.

Moodle does not run Python in a page on its own. Teachers usually add a plugin such as CodeRunner for graded exercises, or link out to an external notebook like Jupyter or Google Colab. With DTeach, you add one Moodle activity and your students open a course where Python cells run in their browser, signed in with their Moodle account and nothing to install.

What are the usual ways to teach Python with Moodle?

Most Moodle sites that teach programming combine a few of these.

CodeRunner for graded code questions

CodeRunner is a question type plugin for the Moodle quiz. Students write code in a question, and Moodle runs it against test cases to grade the answer. It fits exams and graded exercises well. The code usually runs on a separate sandbox server, often Jobe, set up by your Moodle administrator.

Many teachers share a notebook with a URL resource in their Moodle course. Colab runs in the browser and asks students to sign in with a Google account. Jupyter works well when your institution already runs a JupyterHub, or when students install Python on their own machine. Either way, the code lives outside Moodle, away from the lesson.

Screenshots and code in a page

The simplest option is to paste code into a Moodle page or a PDF. Students copy it into their own editor to try it, so the code never runs where the explanation is.

Can students run Python inside a Moodle course?

Yes, with DTeach. DTeach plugs into Moodle over LTI 1.3 through a plugin you install in your Moodle. A teacher adds DTeach as an activity in a Moodle course. Everyone who opens the activity lands in the right DTeach course, already signed in, with the role Moodle gives them. Teachers, teaching assistants, and content developers can edit the course; everyone else opens it as a student. Nobody to invite, no second password.

Inside that course, a code block set to Python runs right in the page. Python runs in each student's browser, so there is nothing to install on their laptop or on your servers.

How do the Python cells work?

They behave like notebook cells, placed between your paragraphs, LaTeX, and shared diagrams:

  • Run runs one cell. A counter such as [1] or [2] shows that it ran.
  • Run all runs every Python cell from the top of the document down to this one, in order.
  • Cells share their variables, so a DataFrame defined in one cell is available in the next.
  • Restart kernel clears every variable and output and starts fresh.
  • A cell that runs longer than 30 seconds is stopped for you.

pandas, numpy, matplotlib and the other common scientific packages install themselves the first time a cell imports them.

import pandas as pd

df = pd.DataFrame({
    "city": ["Lyon", "Lille", "Nantes", "Rennes"],
    "students": [412, 287, 356, 198],
})
df.sort_values("students", ascending=False)

When a cell ends with a pandas DataFrame, Show DataFrame opens the table full size. Students search across every column, sort by clicking a column name, and see each column's type with a small histogram of its values. A matplotlib figure gets a Show image button.

Does one student's code affect the others?

No. Each person runs their own copy: what a student runs does not change what the rest of the class sees. Students edit the code itself only in sections where you turn on student editing. An open section is a shared page that syncs live, which suits an exercise sheet the class fills in together.

How do I set up Python in a Moodle course with DTeach?

A Moodle administrator installs the plugin once for the whole site. Then any teacher adds the activity where it is needed.

  1. Install the plugin. Download the DTeach plugin, then go to Site administration, Plugins, Install plugins and drop the ZIP file in. You need Moodle 4.2 or later and administrator rights.
  2. Activate your licence. In the plugin's Connection page, paste the licence key you received and press Activate. Moodle registers with DTeach on its own.
  3. Add the activity. In a Moodle course, turn editing on, choose Add an activity or resource, then DTeach. Press Select content, then Create a new course, or Link to reuse a course you already built. Save the activity.
  4. Write the lesson. Open the activity as a teacher. In a document, type /code, pick Code Block, and set its language to Python.
  5. Open it as a student. Check the activity with a student account. Students land in the course with read access and can press Run on every cell.

The full walkthrough, with videos, is in Install the Moodle plugin.

When should I use CodeRunner, and when DTeach?

They can sit side by side in the same Moodle course. CodeRunner grades code inside the Moodle quiz. DTeach is where you write and teach the lesson: Python cells next to the explanation, student Q&A anchored on the cell a student is stuck on, and private notes each student keeps for themselves.

DTeach does not send grades to the Moodle gradebook today. Grade passback is on the roadmap, as described in Teaching from Moodle. If a Python exercise must count toward a grade now, a quiz question remains the right tool.

Wrap-up

To run Python in Moodle without installing anything for your students, add DTeach as an activity and write your lesson with Python cells. Start with Python code for what a cell can do, then Use DTeach in Moodle for the connection. The Moodle connection is licensed for your whole Moodle site, priced to your institution, and billed monthly or yearly: see pricing.

FAQ

Do students need to install Python?

No. Python runs in the student's browser when they open the course from Moodle. Common packages such as pandas, numpy, and matplotlib install themselves the first time a cell imports them.

Can I share the same Python course with several Moodle classes?

Yes. Point several DTeach activities at the same course with Link. An edit made once shows everywhere, and each person keeps the role their own Moodle course gives them. See Reuse a course in several places.

Can I publish a Python course outside Moodle?

Yes. Any teacher can create a course on DTeach for free and make it public with one link. Visitors run the Python cells too, without an account. See Publish a course.