serve_with_flask.pyΒΆ

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
"""
Example of serving a Flexx app using a regular web server. In this case flask.
See serve_with_aiohttp.py for a slightly more advanced example.
"""

from flask import Flask

from flexx import flx
from flexxamples.howtos.editor_cm import CodeEditor

# Define an app

class MyApp(flx.Widget):
    def init(self):
        with flx.HBox():
            CodeEditor(flex=1)
            flx.Widget(flex=1)


# Dump it to a dictionary of assets that we can serve. Make the main
# page index.html. The link=0 means to pack the whole app into a single
# html page (note that data (e.g. images) will still be separate).
app = flx.App(MyApp)
assets = app.dump('index.html', link=0)


# Do the flask thing

app = Flask(__name__)

@app.route('/')
def handler():
    return assets['index.html'].decode()


if __name__ == '__main__':
    app.run(host='localhost', port=8080)