flexxui / pscript

Python to JavaScript compiler
http://pscript.readthedocs.io
BSD 2-Clause "Simplified" License
256 stars 25 forks source link

How can I handle imports? #40

Open louisabraham opened 4 years ago

louisabraham commented 4 years ago

Hi, I have a project with several files and modules. Is there a way to convert it simply to pscript?

almarklein commented 4 years ago

You may want to have a look at Flexx. Flexx is able to detect functions/classes being used that are imported from other modules, and resolve everything. As long as your code does not use any unsupported builtins, anything related to the file system, and only pure Python, it should be able to compile everything.

almarklein commented 4 years ago

It's also possible to use just PScript. In that case you just compile each module, and handle imports e.g by writing somemodule = window.somemodule at the top.

louisabraham commented 4 years ago

Hi! First, I have to tell you I loved your talk "Let’s embrace WebAssembly!"

How would I use Flexx to do that?

I suppose imports in functions and dynamic imports are not supported.

I am willing to improve PScript to support imports, is it within the scope of this project? How should I start?

almarklein commented 4 years ago

Hi! First, I have to tell you I loved your talk "Let’s embrace WebAssembly!"

How would I use Flexx to do that?

Thanks :) well, Flexx and WebAssembly are mostly unrelated things. In the talk we've focused more on running WASM in Python, instead of the other way around. For running Python in the browser with WASM, which I assume is what you want to do, can be done in (at least) three ways:

In the first two your code is still Python - only the VM (i.e. the interpreter capable of running Python code) is compiled to wasm. The latter is probably the most interesting, since you'd also gain a speed benefit. But such a compiler does not yet exist :) It would be a bit like PScript, but with types (and therefore more predictable, no hidden weird JS behavior).

I am willing to improve PScript to support imports, is it within the scope of this project? How should I start?

I'm not sure if it is within scope. But you could start by extracting the logic from Flexx and rolling that into a new project. The code is in flexx/app I should warn you though that this Flexx subpackage is where many things come together, so it's not the easiest code to navigate :) though most logic of your interest should be in _modules.py, _assetstore.py and perhaps _session.py.

louisabraham commented 4 years ago

Oh, sorry for the confusion!

"that" = "detect functions/classes being used that are imported from other modules, and resolve everything"

I know quite well about Pyodide (I use it here). I guess your third thing could be achieved with Cython + emscripten :)

So my original question is: how would you transpile a module using flexx?

almarklein commented 4 years ago

Aha :)

how would you transpile a module using flexx?

The exact way to go depends on your goals, but at some point you'll have a main Flexx widget:

class MyMainWidget(flx.Widget):  # or maybe flx.PyWidget
    def init(self):
        super().init()
        # ... this runs in JS.
        # You can call existing Python functions here, and Flexx will pick it up,
        # resolve everything that *that* code is using, etc. and then compile everything.

# Then you turn that into an app object
your_app = flx.App(MyMainWidget)

The app object you can then .serve() or .launch(), so you have a Python server connected over a websocket to the browser. You can then also have the client invoke actions in Python and the other way around.

For a more classical approach (and frankly, an approach that is a bit less magical) you can use your_app.dump() to create a dictionary with all the assets that you can then serve using Flask, Uvicorn, or whatever. Similarly you can .dump() the assets to disk.

The most important question is probably whether you want to convert your app to JS in full, or whether you need a server-client thing going on.