Skip to content

Conversation

@KDW1
Copy link

@KDW1 KDW1 commented Jan 21, 2026

Added an alternative setting for the PathView application so that you can either use Pyodide web assembly or send client-side fetch requests to a Flask web server that has execution, evaluation, and traceback routes (the initialization code of the server borrows from the init() function found in worker.ts.

Major Changes:

  • Added a backendPreferenceStore which is nearly identical in form to themeStore since it switches between two binary values. It is binded to 'Q' (arbitrarily so), and toggling it calls switchBackend(preference : BackendPreference) from backend.ts
  • The backendPreferenceStore is accessed from backend.ts and passed in as an event parameter sent along with the "init" message posted to the worker. This sets the backendPreference for said worker
  • Every function within worker.ts that previously called await pyodide.runPythonAsync(...) now has a fetch request counterpart, such that the Flask backend route is shallow copy of what pyodide was doing

Current issues:

  • It seems like we're running multiple excess requests due to an error Scope: 'NoneType' object is not iterable which comes from the generateStreamingStartCode() function in pythonHelpers.ts, specifically the find_scopes() funciton

KDW1 added 21 commits January 14, 2026 11:38
…lp me identify what routes to replace with the Flask alternative
…ven't implemented nay backend changes), also added backendpref store and structure
…eyboard shortcuts, currently working on modifying handleRun and handleContinue
… out what other data structures (like nodeRegistry and some of the python generation code) I need to convert
…out how to replicate the validateGraphBridge() functionality
…imply redirect pyodide execution requests to the web server!
… -> Pyodide backend -> workers in order to avoid accessing localStorage from the level of the workers in the Pyodide backend
…he store, instead of the value being passed down! Also identified where the rror is thrown about a owrker not being initialized
… seem to have finished eval and exec. Trying to figure out how to run streaming with flask now
@RemDelaporteMathurin RemDelaporteMathurin changed the title Merging flask-backend branch into pathsim Adding a Flask backend Jan 21, 2026
@KDW1
Copy link
Author

KDW1 commented Jan 21, 2026

TODOS

  • Review the requirements for handleRun() in the main index page for the SvelteKit application, in terms of whether simulation is running or not, etc...
  • Fix the issue where the Flask version doesn't interact with the console's stdout and stderror
  • Fix the Scope: 'NoneType' object is not iterable error, which is most likely connected to the excess api calls!
  • Finish pull request description

@milanofthe
Copy link
Member

@KDW1 good start, basic exec/eval works and the toggle UI is there. But there are some architectural issues preventing this from working properly.

Worker imports stores:
Workers run in a separate thread and can't access main thread state. This is why you hit the localStorage/window errors. The typeof window === 'undefined' check masks it but doesn't fix it. Workers must be self-contained, pass state via postMessage.

Flask inside PyodideBackend:
The Backend interface exists so we can swap implementations cleanly. Flask shouldn't be a flag inside PyodideBackend, it should be its own FlaskBackend class implementing the same interface. No worker needed for Flask, just fetch calls from main thread.

Streaming broken:
This is why Scope returns (None, None) or partial data. The pyodide streaming works because the generator _streamer lives in the worker and maintains state between steps. With Flask each HTTP request is independent - the generator state doesn't persist reliably across requests. For Flask streaming you'd need SSE/WebSocket.

Minor:
Typo reosurces in backend.py CORS config, requirements.txt lists stdlib modules (gc, io, contextlib) which will fail pip install.

Suggestion:
Create a FlaskBackend class, remove store imports from worker.ts. Read the readme for the architecture. Or just copy it into chatgpt.

@KDW1
Copy link
Author

KDW1 commented Jan 23, 2026

Thank you for the help Milan. I appreciate the direction. I'll be sure to work on a separate Flask Backend class in backend.ts and address the rest of the issues. Thank you!

@KDW1
Copy link
Author

KDW1 commented Jan 23, 2026

TODOS (After Review)

  • Create FlaskBackend class
  • Remove store imports and flask alternatives in worker.ts to be completely removed from the flask backend class
  • Work on an api route that focus on the Flask API streaming the data with SSE
  • Fix the resources typo

KDW1 added 2 commits January 23, 2026 09:34
…-backend

Updating flask-backend branch of my fork with the most recent changes to main in the original repository
KDW1 added 10 commits January 23, 2026 09:50
…tion. Still need to implement basic streaming option and also work on removing the flask snippets that remain in worker.ts
… a separate Flask Backend class, now I just need to begin working on Flask streaming and removing the Flask code from worker.ts
…ng logic within a single api request and then stream the resulting data!
…is working out but I need to work on fetching those results
…rrently working on the chunk streaming issue
…responses can be streamed within the same chunk
@RemDelaporteMathurin
Copy link
Collaborator

remember to update your branch regularly

image

@milanofthe
Copy link
Member

@KDW1 for switching the backends, I think it would make sense to do that via URL parameter /?backend=pyodide, /?backend=flask, etc. The default should remain pyodide. Additionally the host for the backend could also be configured this way.

@KDW1
Copy link
Author

KDW1 commented Jan 28, 2026

Thank you @milanofthe, sounds good I will add the backend preference switching to check for the search query parameter. Also, I may need more time to finish this as I realized that I messed up the namespace on the backend. Right now, I'm trying to add session-specific namesapaces so that clients with different session will have different namespaces. Before, I don't know exactly how it worked but I modified a global variable in the backend.py though I think that will be problematic which is why I'm trying to work with Flask sessions

@KDW1
Copy link
Author

KDW1 commented Jan 30, 2026

I just finished the functionality that allows Pyodide to be the default while Flask is switched to via a URL parameter ?backend=flask. I also fixed the issue with user-specific namespaces by adding a dictionary of different user ids that correspond to individual browsers' namespaces. For this, I just need to work on two things:

  1. Some /terminate API endpoint that's run when the browser closes or some other system, such that I can delete namespaces that aren't needed anymore (as the client has left the application)
  2. An expiry for namespaces so that after some amount of time since their last use, they are deleted. I'll probably make this a function like cleanUpServerNamespaces(), which is called whenever initialization, execution, or evaluation proceeds.

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.

3 participants