Skip to content

Add canvas support to pyide#1123

Merged
mikebarkmin merged 24 commits into
mainfrom
copilot/add-canvas-support-to-pyide
May 15, 2026
Merged

Add canvas support to pyide#1123
mikebarkmin merged 24 commits into
mainfrom
copilot/add-canvas-support-to-pyide

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented May 8, 2026

  • Add canvas support to pyide
  • Add canvas example to the website pyide documentation
  • Fix: after hitting run, the editor no longer receives automatic focus
  • Fix: editor is usable after an exception (don't blur activeElement in releaseKeyboardCapture)
  • Fix: call pygame.quit() after canvas execution so keyboard event listeners are released even when a Python exception occurs

@vercel
Copy link
Copy Markdown

vercel Bot commented May 8, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
hyperbook-4fwa Ready Ready Preview, Comment May 15, 2026 10:41am

Copilot AI linked an issue May 8, 2026 that may be closed by this pull request
Copilot AI changed the title [WIP] Add canvas support to pyide Add canvas support to pyide May 8, 2026
Copilot AI requested a review from mikebarkmin May 8, 2026 21:57
@mikebarkmin
Copy link
Copy Markdown
Member

@copilot add an example to the website for element pyide.

@mikebarkmin mikebarkmin marked this pull request as ready for review May 9, 2026 15:22
mikebarkmin and others added 4 commits May 10, 2026 00:18
- Add unProxy() helper to convert Pyodide PyProxy objects to JavaScript objects
- Update js_graphic_size() to unproxy specs parameter
- Update js_render_graphic() to unproxy specs parameter
- Fixes issue where Pytamaro graphic specs weren't being processed correctly

This implementation is based on the reference implementation pattern used in
other Pytamaro-Pyodide integrations, which properly handles PyProxy objects
returned from Python code.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
- Add error logging to unProxy() helper for debugging
- Wrap FFI functions in try-catch blocks with console.error logging
- Fix incomplete catch block in js_render_graphic
- Improves debugging when Pytamaro rendering fails

This helps identify issues when PyProxy object conversion or graphic
rendering fails, making it easier to diagnose Pytamaro integration problems.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
- Parse @@@PYTAMARO_DATA_URI_BEGIN/END@@@ markers in stdout and render
  as <img> elements instead of plain text
- Fix dict_converter: Object.fromEntries in unProxy() so Python dicts
  are converted to plain JS objects (not Maps), allowing buildGraphic()
  to access spec.t, spec.width, spec.color etc. correctly
- Remove incorrect {canvas} attribute from pytamaro pyide examples
  (pytamaro renders via stdout/output tab, not the pygame canvas tab)
- Fix heading typo Pytamato -> Pytamaro in English docs
- Add missing Pytamaro section to German docs

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copy link
Copy Markdown
Contributor Author

Copilot AI commented May 15, 2026

@copilot After hitting run the editor should not get automatic focus.

Fixed in a14bca3. Removed the editor?.focus?.() call from releaseKeyboardCapture so the editor no longer steals focus after clicking run (or stop).

@mikebarkmin
Copy link
Copy Markdown
Member

@copilot after an exception I can not type in the code editor

Copy link
Copy Markdown
Contributor Author

Copilot AI commented May 15, 2026

@copilot after an exception I can not type in the code editor

Fixed in cdbb0b7. The previous fix inadvertently called document.activeElement?.blur?.() which blurred the editor's textarea on exception — leaving the code-input component in a non-interactive state without a re-focus to recover it. Now releaseKeyboardCapture only blurs the canvas element (to release canvas keyboard capture), so the editor remains usable after an exception without auto-focusing it.

@mikebarkmin
Copy link
Copy Markdown
Member

@copilot I was talking about an exception in python:

Loading pygame-ce
Loaded pygame-ce
pygame-ce 2.5.6.dev2 (SDL 2.32.0, Python 3.13.2)
Traceback (most recent call last):
File "", line 29, in
File "/lib/python313.zip/pyodide/webloop.py", line 961, in _run
return loop.run_until_complete(main)
~~~~~~~~~~~~~~~~~~~~~~~^^^^^^
File "/lib/python313.zip/pyodide/webloop.py", line 369, in run_until_complete
return run_sync(future)
File "", line 22, in run_game
NameError: name 'sys' is not defined. Did you forget to import 'sys'?

For this code:

import pygame
import asyncio

async def run_game():
fps = 60
pygame.init()
screen = pygame.display.set_mode((400, 300))
r = 0
g = 0
b = 0

while True:
    for event in pygame.event.get():
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_r:
                r = (r + 50) % 256
            elif event.key == pygame.K_g:
                g = (g + 50) % 256
            elif event.key == pygame.K_b:
                b = (b + 50) % 256
            elif event.key == pygame.K_ESCAPE:
                sys.exit()
        elif event.type == pygame.QUIT:
            return
    screen.fill((r, g, b))
    pygame.display.flip()
    await asyncio.sleep(1 / fps)

asyncio.run(run_game())

this happens in pyide.md

Copilot AI and others added 2 commits May 15, 2026 10:00
Copy link
Copy Markdown
Contributor Author

Copilot AI commented May 15, 2026

@copilot I was talking about an exception in python:

Loading pygame-ce
...

Fixed in 092751e (cleaned up in 3904fd3). When a Python/pygame-ce exception occurs, pygame.quit() is never reached in the user's code, so pygame's keyboard event listeners remain registered on the canvas element and block typing in the editor. The fix adds a cleanup step in the finally block of executeScript that calls pygame.quit() via pyodide whenever canvas execution ends — whether the script succeeded or threw a Python exception.

mikebarkmin and others added 2 commits May 15, 2026 12:35
- All IDE directives (pyide, typst, webide, p5, onlineide, sqlide) now
  take the full viewport height (calc(100dvh - 80px)) on all screen
  sizes, not just wide screens
- Add height attribute support to pyide, typst, webide, p5 via CSS
  custom property (--X-height) on the root element
- onlineide and sqlide default height changed from 600px to
  calc(100dvh - 80px); height attribute sets CSS variable on root
- Inner panels use flex: 1 1 0 to share the root height proportionally
  in column (small-screen) layout
- Redundant height declarations in media queries removed

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@mikebarkmin mikebarkmin merged commit 5607041 into main May 15, 2026
3 checks passed
@mikebarkmin mikebarkmin deleted the copilot/add-canvas-support-to-pyide branch May 15, 2026 10:48
@github-actions github-actions Bot mentioned this pull request May 15, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add canvas support to pyide

2 participants