Skip to content

Commit dfe86a9

Browse files
ZDStudiosclaude
andcommitted
Browser demo reads Python, and grows to 95 blocks
Two things the demo could not do: it had a small handful of blocks, and it could only be built by hand. Reading Python in a browser means no ast module, so this adds a tokeniser and a recursive descent parser written from scratch in JavaScript. It handles indentation, strings including triple quoted and f-strings, joined lines, and the usual precedence ladder. On top of that sits a mapper to blocks that understands assignments, if/elif/else, while, for over range and over lists, def with parameters and returns, break, continue, comparisons, and/or/not, list and string methods and the built-ins people actually use. Functions arrive as real custom blocks with a define hat and a call block. Anything unrecognised is kept word for word, so no file is refused. Press Import .py, paste a program, or drop a file anywhere on the page. The block set goes from 26 to 95 across eight categories: lists, custom blocks and a Python category join the original five, and the existing ones fill out with the operators, text handling and control blocks the desktop app has. Everything still runs in the page. Six programs are tested three ways: run by real Python, imported into blocks and run by the demo's interpreter, then turned back into Python and run again. All three agree exactly, which turned up four real faults on the way: numeric slots trimming the text they were given, print with three or more arguments falling back to raw code, list literals never reaching the interpreter, and len() of a plain variable doing the same. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
1 parent 443d7e0 commit dfe86a9

2 files changed

Lines changed: 2026 additions & 137 deletions

File tree

README.md

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,11 @@ python scratchpy_studio.py
3333

3434
### 👉 [Try the browser demo](https://zdstudios.github.io/scratchpy-studio/) — no install, nothing to download
3535

36-
[`index.html`](index.html) is a small standalone version of the same idea: one
37-
file, no dependencies, no build step. Drag blocks, watch the Python appear, press
38-
Run and it executes right there in the page. It is a taste of the real thing —
39-
the desktop app has 148 blocks, pip, file handling and the rest.
36+
[`index.html`](index.html) is a standalone version of the same idea: one file, no
37+
dependencies, no build step. 95 blocks, drag and snap, the Python appearing live
38+
beside you, Run working right there in the page — and you can **drop a `.py` file
39+
on it** and watch the whole program turn into blocks. The desktop app adds pip,
40+
virtual environments, files, the web blocks and the MCP server.
4041

4142
---
4243

@@ -346,23 +347,42 @@ finally runs the generated example program and checks its output.
346347
double-clicking it, or visit at
347348
**[zdstudios.github.io/scratchpy-studio](https://zdstudios.github.io/scratchpy-studio/)**.
348349

349-
It shares the desktop app's ideas in about 1,600 lines of HTML, CSS and
350+
It shares the desktop app's ideas in about 3,400 lines of HTML, CSS and
350351
JavaScript with **no dependencies and no build step**:
351352

352-
* the same Scratch 3 shapes, drawn as SVG paths from the same puzzle geometry
353+
* **95 blocks** across eight categories — the same Scratch 3 shapes, drawn as SVG
354+
paths from the same puzzle geometry
353355
* one continuous drawer that glides between sections
354356
* drag, snap, C-shaped mouths, reporters that drop into slots
355357
* the Python it makes, updating live beside you — copy it or save it as a `.py`
356358
* a small interpreter so **Run** actually works in the page, and clicking a loose
357359
block still shows its answer in a bubble
360+
* **it reads Python too** — see below
358361

359362
Each block is described once, and that single description drives all three
360363
things: how it is drawn, the Python it generates, and how it runs. It works with
361364
a mouse, a pen or a finger.
362365

363-
The demo keeps 26 blocks across five categories. Everything else — the other
364-
120 blocks, pip, the venv, files, the web blocks, the Python importer, the MCP
365-
server — lives in the desktop app.
366+
### Import a Python file, in the browser
367+
368+
Press **Import .py**, paste a program, or just drop a `.py` file onto the page.
369+
A small tokeniser and recursive-descent parser — written from scratch in
370+
JavaScript, since browsers have no `ast` module — reads the file and builds the
371+
blocks.
372+
373+
It understands assignments, `if`/`elif`/`else`, `while`, `for` over `range` and
374+
over lists, `def` with parameters and returns, `break`, `continue`, f-strings,
375+
comparisons, `and`/`or`/`not`, list and string methods, and the usual built-ins.
376+
Functions become real **custom blocks** with their own define hat and call block.
377+
Anything it does not recognise is kept word for word in a "python" block, so no
378+
file is ever refused.
379+
380+
> Six programs were tested three ways: run by real Python, imported into blocks
381+
> and run by the demo's own interpreter, then turned back into Python and run
382+
> again. All three produce **identical output** every time.
383+
384+
What the demo leaves out: pip, virtual environments, files, the web blocks and
385+
the MCP server. Those need a machine, not a tab — they are in the desktop app.
366386

367387
---
368388

0 commit comments

Comments
 (0)