Ways to run a Flexx app¶
Run as a desktop app¶
During development, and when creating a web app, you will want to use
app = flx.App(MainComponent) app.launch('app') # to run as a desktop app # app.launch('browser') # to open in the browser flx.run() # mainloop will exit when the app is closed
Flexx in Jupyter¶
Flexx can be used interactively from the Jupyter notebook.
flx.init_notebook() which will inject the necessary JS and CSS.
%gui asyncio to enable the Flexx event system.
Simple widgets (e.g. buttons) will display just fine, but for other
widgets you might want to use
specify a minimum size.
As of yet, Flexx does not work in JupyterLab.
Serve as a web app¶
It is possible to serve Flexx apps and allow multiple people to connect. Flexx provides ways to have all connected clients interact with each-other, see e.g. the chatroom and colab-painting examples.
app = flx.App(MainComponent) app.serve('foo') # Serve at http://domain.com/foo app.serve('') # Serve at http://domain.com/ flx.start() # Keep serving "forever"
Each server process hosts on a single URL (domain+port), but can serve multiple applications (via different paths). Each process uses one tornado IOLoop, and exactly one Tornado Application object. Flexx’ event loop is based on asyncio (Tornado is set up to integrate with asyncio).
The overhead for each connection is larger than that of classic http frameworks, and the complexity of the Python-JS interaction are a potential risk for security issues and memory leaks. For the Flexx demo page we run the server in an auto-restarting Docker container with applied memory limits.
Export to a static web page¶
Any app can be exported to its raw assets. However, apps that rely on a PyComponent won’t work correctly, obviously. Exporting to a single file won’t work for apps that use session/shared data.
app = flx.App(MainComponent) app.export('~/myapp/index.html') # creates a few files app.export('~/myapp.html', link=0) # creates a single file
Serve as a proper web app¶
When creating an app that will run on a long-running server and/or will be accessed by many clients, you may want to implement the server using an http framework such as aiohttp, flask, etc.
You can first dump the assets to a dictionary, which you can then serve
using your library of choice. See the
serve_with_ examples for
It’s worth noting that if
flx.start() are not called, Flexx won’t even import Tornado (and
we have a test to make sure that it stays that way). This makes it
feasible to generate the client-side of a website with Flexx when the
server starts up.
app = flx.App(MainComponent) assets = app.dump('index.html') ... # serve assets with flask/aiohttp/tornado/vibora/django/...