-
Notifications
You must be signed in to change notification settings - Fork 485
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Python Lab] add support for matplotlib and small updates #57782
Conversation
@@ -102,7 +102,22 @@ const PythonEditor: React.FunctionComponent = () => { | |||
<div> | |||
Output: | |||
{codeOutput.map((outputLine, index) => { | |||
return <div key={index}>{outputLine}</div>; | |||
if (outputLine.type === 'img') { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
non-blocking nit: Should we eventually move these types to a constants file somewhere?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is going to get refactored completely so I think for now I'll leave as-is
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
but yes, they should eventually be constants :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Haha, I figured! As I typed it I knew I was just making the "obligatory seeing plaintext in a condition" comment 😂 Sounds good to me!
return ( | ||
<img | ||
key={index} | ||
src={`data:image/png;base64,${outputLine.contents}`} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice solution!
os.fsync(sys.stdout.fileno()) | ||
`; | ||
|
||
// TODO: Should we always patch matplotlib? Or can we be smarter about when to patch it? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there some comment about this included in the task to productionize the patch? If not, can we add a comment?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll add a link to the ticket here!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice work! 🎉
Warning!!
The AP CSP Create Performance Task is in progress. The most critical dates are from April 3 - April 30, 2024. Please consider any risk introduced by this PR that could affect our students taking AP CSP. Code.org students taking AP CSP primarily use App Lab for their Create Task, however a small percent use Game Lab. Carefully consider whether your change has any risk of alterering, changing, or breaking anything in these two labs. Even small changes, such as a different button color, are considered significant during this time period. Reach out to the Student Learning team or Curriculum team for more details.
Summary
The work in this PR came out of investigating if we could get matplotlib working in Python Lab. matplotlib is a supported pyodide package, but there are some issues with getting it working in a web worker (see this issue for details). As a simple workaround we can patch the matplotlib
show
function to print a base64 string, then show said string in an image element. This may prove to be a little fragile; I filed a follow up task to productionize this.This work required a couple things that caused me to do a few refactors/improvements more generally to Python Lab. These are:
pyodideWorkerManager
. Now I have moved the existing flush system out patch (which is appended to all student code) and the new matplotlib patch into a separate file, and created a utils file to handle wrapping the user's code.This is what it looks like to run a program that outputs two graphs
![Screenshot 2024-04-03 at 2 34 41 PM](https://private-user-images.githubusercontent.com/33666587/319374406-eac25fc2-daa0-42f1-8eaf-3b1be7c23aa7.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjU5ODIwNTgsIm5iZiI6MTcyNTk4MTc1OCwicGF0aCI6Ii8zMzY2NjU4Ny8zMTkzNzQ0MDYtZWFjMjVmYzItZGFhMC00MmYxLThlYWYtM2IxYmU3YzIzYWE3LnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNDA5MTAlMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjQwOTEwVDE1MjIzOFomWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPThhMGQ5NTM5OTBiZmIyNzE0YTE4NjA1NzhiNDZlYTIwZjViMDNiMGIyOTNiMjcxNzk2N2QzMThjYTRlZmQ5ZjYmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0JmFjdG9yX2lkPTAma2V5X2lkPTAmcmVwb19pZD0wIn0.9M9Be-6f0oI936aj_unxeGNnm1eu2jMDzgQC2bw7TAM)
Links
Testing story
Tested locally. Python Lab is isolated code and not in use, so this is low risk.
Follow-up work
PR Checklist: