Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How can I handle imports? #40

Open
louisabraham opened this issue Dec 17, 2019 · 6 comments
Open

How can I handle imports? #40

louisabraham opened this issue Dec 17, 2019 · 6 comments
Labels
question Further information is requested

Comments

@louisabraham
Copy link

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

@almarklein
Copy link
Member

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 almarklein added the question Further information is requested label Dec 18, 2019
@almarklein
Copy link
Member

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
Copy link
Author

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
Copy link
Member

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:

  • Compile CPython (and other libs) to WASM, see Pyodide.
  • Run your code in RustPython compiled to WASM.
  • Compile typed Python code to WASM.

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
Copy link
Author

louisabraham commented Dec 19, 2019

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
Copy link
Member

almarklein commented Dec 23, 2019

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants