保存一个来自pyscript py-repl环境的REPL会话。

huangapple go评论57阅读模式
英文:

Save a REPL session from pyscript py-repl environment

问题

我正在创建一个与谷歌课堂集成的GAS网络应用,供我的学生提交Python作业。基本设计使用pyscript的py-repl接口,供学生尝试解决问题。我想要能够做的是复制py-repl的内容,基本上是Python REPL会话,然后我将使用它来创建一个文本文件放在学生的谷歌云端硬盘中,并将该文本文件提交给谷歌课堂的作业。我遇到的问题是,我似乎无法弄清楚如何获取会话。似乎有一种将stdout和stderr重定向到HTML标签的方法,我可以将其设置为隐藏,并从DOM中获取内部文本,但那不是我想要的。我想要的是学生在REPL中键入的代码行。

英文:

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.

答案1

得分: 1

有几种选择,其中之一基于内部,另一种将在下一个版本中可能实现。

py-repl元素具有getPySrc()方法,用于获取REPL的当前代码内容。这是当前py-repl的实现细节,但已经相当稳定。实际上,已经有一个跟踪此问题的链接,作为一个请求的官方API功能。

PyScript的下一个版本(目前在发布候选2中)引入了两种新的插件方法beforePyReplExec()afterPyReplExec(),它们在REPL中的Python代码执行之前和之后分别被调用。它们接收的参数之一是一个src参数,其中包含即将(或刚刚)执行的源代码。您可以使用这个来跟踪每个REPL单元格中最后执行的代码,并记录下来以供提交。可能比您需要的复杂,但它会存在。

英文:

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.

huangapple
  • 本文由 发表于 2023年6月8日 10:33:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/76428228.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定