plotly / dash

Data Apps & Dashboards for Python. No JavaScript Required.
https://plotly.com/dash
MIT License
21.27k stars 2.05k forks source link

provide Pyodide backend for Dash (for serverless Dash) #1559

Open jmsmdy opened 3 years ago

jmsmdy commented 3 years ago

One approach to allow Dash to run serverless is to reimplement the backend to be able to run in a web worker running pyodide, and modify all requests between frontend and backend to use messages to/from the web worker. From what I understand, the Dash backend and frontend are highly decoupled and all data is already passed in some kind of JSON format, so this should not require any major architectural changes.

This could have a number of advantages, including drastically improving the performance of data-heavy Dash apps on slow networks, and allowing Dash apps to be "exported" to be served by a much lighter weight server.

I have already submitted a pull request to make plotly.py compatible with pyodide (a very simple change, which is scheduled to be merged in the 4.15.0 release). As far as I can see, the changes to Dash required are as follows:

  1. Any code in dash-renderer that generates requests should be modified to allow sending messages to a web worker if an optional parameter is set.

    a) I do not know how dash-renderer interfaces with extensions to Dash, but I am hoping extensions such as a dash_bootstrap_components, etc. would not need to be modified. If this is not the case, the priority would be to focus on just the core dash components, and then modify extensions over a longer period of time.

    b) In the best case scenario, dash-renderer has already modularized the code for generating requests to the server, so the changes would be limited to only a small part of the code. If this is not the case, then this modularization would have to happen first.

  2. The backend needs to be rewritten to replace Flask with a system that can run in a web worker with pyodide.

    a) I expect that this would not require anything close to a complete rewrite. It cannot be accomplished by a simple wrapper around Flask which intercepts/redirects requests, since Flask cannot run in pyodide (the required networking apis are not available there). However, most of the Dash backend code seems like it could be reused as is.

    b) It would require a separate build of the backend for pyodide. Perhaps this would require putting all Flask-related parts of the backend code into separate files and doing some rewriting so that maintenance of the non-Flask parts of the backend doesn't need to be duplicated.

  3. Develop some convenience code for switching between loading local/remote data sources depending on execution context (backend running on a server where data files are "local" versus in Pyodide on a web worker where they need to be pulled in from a remote location) to make it easy to switch deployment targets.

  4. Write new code to enable easy export of Dash apps to use a pyodide backend as a complete package. This would package the code generated by dash-renderer with a version of the new pyodide backend so that it can be hosted statically.

I'm willing to put most of the work into this myself (as long as there are no major issues I missed). I am not familiar with the Dash codebase, so I would likely need some help getting a picture of the architecture, as well as some guidance to ensure that the work I am proposing does not interfere with any other plans for Dash or structural principles.

rth commented 2 years ago

There is an implementation of this in https://github.com/ibdafna/webdash

gvwilson commented 1 month ago

@jmsmdy can you please let me know if this is still a concern?

sslivkoff commented 1 week ago

@jmsmdy can you please let me know if this is still a concern?

a pyodide backend would be extremely useful. being able to run the relevant python code inside the browser means that the backend could be completely eliminated. this would make dash much easier to deploy because you would not longer need to rent or configure or maintain servers. you could also share dash dashboards as a plain directory of html/js/css files that can be directly opened in the browser with no server required

the webdash project is a cool experiment. but it looks like it is no longer active. it would be very nice to have pyodide given first class support as an official backend

CNFeffery commented 1 week ago

@sslivkoff I saw the concrete implementation in py.cafe, for example: https://py.cafe/CNFeffery/fac-getting-started-demo-en-us