|
2 | 2 | import traceback |
3 | 3 | from io import open |
4 | 4 |
|
| 5 | +from gevent.threading import Timer |
5 | 6 | import gevent as gvt |
6 | 7 | import json as jsn |
7 | 8 | import bottle as btl |
|
26 | 27 | _js_functions = [] |
27 | 28 | _mock_queue = [] |
28 | 29 | _mock_queue_done = set() |
| 30 | +_shutdown = None |
29 | 31 |
|
30 | 32 | # The maximum time (in milliseconds) that Python will try to retrieve a return value for functions executing in JS |
31 | 33 | # Can be overridden through `eel.init` with the kwarg `js_result_timeout` (default: 10000) |
|
46 | 48 | 'app_mode': True, # (Chrome specific option) |
47 | 49 | 'all_interfaces': False, # Allow bottle server to listen for connections on all interfaces |
48 | 50 | 'disable_cache': True, # Sets the no-store response header when serving assets |
| 51 | + 'default_path': 'index.html', # The default file to retrieve for the root URL |
49 | 52 | 'app': btl.default_app(), # Allows passing in a custom Bottle instance, e.g. with middleware |
50 | 53 | } |
51 | 54 |
|
@@ -192,6 +195,9 @@ def _eel(): |
192 | 195 | _set_response_headers(btl.response) |
193 | 196 | return page |
194 | 197 |
|
| 198 | +def _root(): |
| 199 | + return _static(_start_args['default_path']) |
| 200 | + |
195 | 201 | def _static(path): |
196 | 202 | response = None |
197 | 203 | if 'jinja_env' in _start_args and 'jinja_templates' in _start_args: |
@@ -235,6 +241,7 @@ def _websocket(ws): |
235 | 241 |
|
236 | 242 | BOTTLE_ROUTES = { |
237 | 243 | "/eel.js": (_eel, dict()), |
| 244 | + "/": (_root, dict()), |
238 | 245 | "/<path:path>": (_static, dict()), |
239 | 246 | "/eel": (_websocket, dict(apply=[wbs.websocket])) |
240 | 247 | } |
@@ -343,17 +350,24 @@ def _expose(name, function): |
343 | 350 | _exposed_functions[name] = function |
344 | 351 |
|
345 | 352 |
|
| 353 | +def _detect_shutdown(): |
| 354 | + if len(_websockets) == 0: |
| 355 | + sys.exit() |
| 356 | + |
| 357 | + |
346 | 358 | def _websocket_close(page): |
| 359 | + global _shutdown |
| 360 | + |
347 | 361 | close_callback = _start_args.get('close_callback') |
348 | 362 |
|
349 | 363 | if close_callback is not None: |
350 | 364 | sockets = [p for _, p in _websockets] |
351 | 365 | close_callback(page, sockets) |
352 | 366 | else: |
353 | | - # Default behaviour - wait 1s, then quit if all sockets are closed |
354 | | - sleep(1.0) |
355 | | - if len(_websockets) == 0: |
356 | | - sys.exit() |
| 367 | + if _shutdown: |
| 368 | + _shutdown.kill() |
| 369 | + |
| 370 | + _shutdown = gvt.spawn_later(1.0, _detect_shutdown) |
357 | 371 |
|
358 | 372 |
|
359 | 373 | def _set_response_headers(response): |
|
0 commit comments