Search code examples
javascriptpythonhtmlpyscript

Save a REPL session from pyscript py-repl environment


I am creating a GAS web app that integrates with google classroom for my students to submit python assignments. The basic design uses pyscript's py-repl interface for students to attempt to solve problems. What I want to be able to do is copy the contents of the py-repl, basically the python repl session, which I will then use to create a text file in the student's google drive, and submit that text file to the assignment in google classroom. The snag I've run into is that I cannot seem to figure out how to grab the session. There seems to be a way to redirect stdout and stderr to an html tag which I could set as hidden, and grab the innner text from the DOM, but that's not really what I want. I want the lines of code that the student typed into the repl.


Solution

  • There are a couple of options here, one of which is based on internals, the other of which will be possible in the next release.

    The py-repl element has a getPySrc() method which fetches the current code contents of the repl. This is an implementation detail of the current py-repl, but has been fairly stable detail. There's actually an issue tracking this as a requested official API feature.

    The next release of PyScript (currently in Release Candidate 2) introduces two new plugin methods, beforePyReplExec() and afterPyReplExec(), which are called just before and just after the Python code in a REPL executes. Among the arguments they receieve is a src argument, which contains the source that is about to be (or just was) executed. You could use this to track, say, the last executed code in each REPL cell, and log that for submittal? Possibly more complicated than you need, but it will be there.