Python Tutor + Jupyter Notebooks
Over the last two years of doing Zoom lectures for hundreds of introductory Python students, one of the things I’m always looking to do is reduce platforms. We used to use repl.it for simple scripting tutorials, Python Tutor for in-depth code tracing discussions, and occasionally Jupyter when going over file reading and HTML. However, recently the platform used to host homework and exams for the course (PrairieLearn) has integrated support for Jupyter Hub and the students now use that platform exclusively for their labs and as the IDE they are given for exams and quizzes. As such, I’ve largely abandoned repl.it is for lectures in favor of Jupyter. The reasons for this are twofold: 1) I imagine it’s distracting for students to have to watch me switch tabs and 2) I don’t like switching tabs. It’s much easier for me to have the simple scripting process in an environment the students are already familiar with and have all visualizations present in that same environment. As such, I came up with this little function to take the source from a cell that just ran in Jupyter and get an iFrame which can be embedded and run in a Jupyter notebook itself.
This function reads the source code from the second to last cell that was run, uses urllib to transform it into a valid string for the URL, and then fetches an iframe of Python Tutor with the provided source code. The reason I read from the second to last cell that was run is my usual setup is quite similar to the image displayed at the top of the page. I like to have a cell setup with a function, a test case, and an example function call so I can script freely without having to bother with the visualization. However, when a student asks a question or I want to demonstrate something that would be better served with Python Tutor all I have to do is run the cell immediately below which contains a function call to render_pytutor().
Use the following link to get a sample Google Colab file demonstrating this tool: https://drive.google.com/file/d/1dLsPFNuoKaRKp7Ho-w8JaO7x9g2oD92r/view?usp=sharing
And the Gist: https://gist.github.com/CoffeePoweredComputers/4e3304c532c0a539d7dfcea50f08db03