Skip to content
This repository was archived by the owner on Jun 22, 2025. It is now read-only.

Commit 649214f

Browse files
Merge pull request #573 from Sentinent/eel-routes-on-custom-app
Add eel routes to `Bottle` instances passed in to `eel.start`
2 parents 6b01349 + c83f4f8 commit 649214f

File tree

7 files changed

+62
-6
lines changed

7 files changed

+62
-6
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# Change log
22

3+
### v0.15.2
4+
* Adds `register_eel_routes` to handle applying Eel routes to non-Bottle custom app instances.
5+
36
### v0.15.1
47
* Bump bottle dependency from 0.12.13 to 0.12.20 to address the critical CVE-2022-31799 and moderate CVE-2020-28473.
58

README.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,7 @@ As of Eel v0.12.0, the following options are available to `start()`:
116116
- **position**, a tuple of ints specifying the (left, top) of the main window in pixels *Default: `None`*
117117
- **geometry**, a dictionary specifying the size and position for all windows. The keys should be the relative path of the page, and the values should be a dictionary of the form `{'size': (200, 100), 'position': (300, 50)}`. *Default: {}*
118118
- **close_callback**, a lambda or function that is called when a websocket to a window closes (i.e. when the user closes the window). It should take two arguments; a string which is the relative path of the page that just closed, and a list of other websockets that are still open. *Default: `None`*
119-
- **app**, an instance of Bottle which will be used rather than creating a fresh one. This can be used to install middleware on the
120-
instance before starting eel, e.g. for session management, authentication, etc.
119+
- **app**, an instance of Bottle which will be used rather than creating a fresh one. This can be used to install middleware on the instance before starting eel, e.g. for session management, authentication, etc. If your `app` is not a Bottle instance, you will need to call `eel.register_eel_routes(app)` on your custom app instance.
121120
- **shutdown_delay**, timer configurable for Eel's shutdown detection mechanism, whereby when any websocket closes, it waits `shutdown_delay` seconds, and then checks if there are now any websocket connections. If not, then Eel closes. In case the user has closed the browser and wants to exit the program. By default, the value of **shutdown_delay** is `1.0` second
122121

123122

eel/__init__.py

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -169,9 +169,11 @@ def run_lambda():
169169
HOST = _start_args['host']
170170

171171
app = _start_args['app'] # type: btl.Bottle
172-
for route_path, route_params in BOTTLE_ROUTES.items():
173-
route_func, route_kwargs = route_params
174-
btl.route(path=route_path, callback=route_func, **route_kwargs)
172+
173+
if isinstance(app, btl.Bottle):
174+
register_eel_routes(app)
175+
else:
176+
register_eel_routes(btl.default_app())
175177

176178
return btl.run(
177179
host=HOST,
@@ -264,6 +266,19 @@ def _websocket(ws):
264266
"/eel": (_websocket, dict(apply=[wbs.websocket]))
265267
}
266268

269+
def register_eel_routes(app):
270+
'''
271+
Adds eel routes to `app`. Only needed if you are passing something besides `bottle.Bottle` to `eel.start()`.
272+
Ex:
273+
app = bottle.Bottle()
274+
eel.register_eel_routes(app)
275+
middleware = beaker.middleware.SessionMiddleware(app)
276+
eel.start(app=middleware)
277+
'''
278+
for route_path, route_params in BOTTLE_ROUTES.items():
279+
route_func, route_kwargs = route_params
280+
app.route(path=route_path, callback=route_func, **route_kwargs)
281+
267282
# Private functions
268283

269284
def _safe_json(obj):
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import eel
2+
import bottle
3+
# from beaker.middleware import SessionMiddleware
4+
5+
app = bottle.Bottle()
6+
@app.route('/custom')
7+
def custom_route():
8+
return 'Hello, World!'
9+
10+
eel.init('web')
11+
12+
# need to manually add eel routes if we are wrapping our Bottle instance with middleware
13+
# eel.add_eel_routes(app)
14+
# middleware = SessionMiddleware(app)
15+
# eel.start('index.html', app=middleware)
16+
17+
eel.start('index.html', app=app)
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<!DOCTYPE html>
2+
<html>
3+
4+
<head>
5+
<title>Hello, World!</title>
6+
</head>
7+
8+
</html>

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
setup(
88
name='Eel',
9-
version='0.15.1',
9+
version='0.15.2',
1010
author='Python Eel Organisation',
1111
author_email='python-eel@protonmail.com',
1212
url='https://github.com/python-eel/Eel',

tests/integration/test_examples.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,3 +61,17 @@ def test_06_jinja_templates(driver: webdriver.Remote):
6161

6262
driver.find_element_by_css_selector('a').click()
6363
WebDriverWait(driver, 2.0).until(expected_conditions.presence_of_element_located((By.XPATH, '//h1[text()="This is page 2"]')))
64+
65+
66+
def test_10_custom_app(driver: webdriver.Remote):
67+
# test default eel routes are working
68+
with get_eel_server('examples/10 - custom_app_routes/custom_app.py', 'index.html') as eel_url:
69+
driver.get(eel_url)
70+
# we really need to test if the page 404s, but selenium has no support for status codes
71+
# so we just test if we can get our page title
72+
assert driver.title == 'Hello, World!'
73+
74+
# test custom routes are working
75+
with get_eel_server('examples/10 - custom_app_routes/custom_app.py', 'custom') as eel_url:
76+
driver.get(eel_url)
77+
assert 'Hello, World!' in driver.page_source

0 commit comments

Comments
 (0)