4
4
import json
5
5
import logging
6
6
from asyncio import Queue as AsyncQueue
7
+ from dataclasses import dataclass
7
8
from queue import Queue as ThreadQueue
8
9
from threading import Event as ThreadEvent
9
10
from threading import Thread
24
25
from gevent import pywsgi
25
26
from geventwebsocket .handler import WebSocketHandler
26
27
from geventwebsocket .websocket import WebSocket
27
- from typing_extensions import TypedDict
28
28
29
29
import idom
30
30
from idom .config import IDOM_WEB_MODULES_DIR
@@ -53,8 +53,8 @@ def configure(
53
53
options: Options for configuring server behavior
54
54
app: An application instance (otherwise a default instance is created)
55
55
"""
56
- options = _setup_options ( options )
57
- blueprint = Blueprint ("idom" , __name__ , url_prefix = options [ " url_prefix" ] )
56
+ options = options or Options ( )
57
+ blueprint = Blueprint ("idom" , __name__ , url_prefix = options . url_prefix )
58
58
_setup_common_routes (blueprint , options )
59
59
_setup_single_view_dispatcher_route (app , options , component )
60
60
app .register_blueprint (blueprint )
@@ -126,48 +126,33 @@ def use_scope() -> dict[str, Any]:
126
126
return use_request ().environ
127
127
128
128
129
- class Options (TypedDict , total = False ):
129
+ @dataclass
130
+ class Options :
130
131
"""Render server config for :class:`FlaskRenderServer`"""
131
132
132
- cors : Union [bool , Dict [str , Any ]]
133
+ cors : Union [bool , Dict [str , Any ]] = False
133
134
"""Enable or configure Cross Origin Resource Sharing (CORS)
134
135
135
136
For more information see docs for ``flask_cors.CORS``
136
137
"""
137
138
138
- import_name : str
139
- """The module where the application instance was created
140
-
141
- For more info see :class:`flask.Flask`.
142
- """
143
-
144
- redirect_root_to_index : bool
139
+ redirect_root : bool = True
145
140
"""Whether to redirect the root URL (with prefix) to ``index.html``"""
146
141
147
- serve_static_files : bool
142
+ serve_static_files : bool = True
148
143
"""Whether or not to serve static files (i.e. web modules)"""
149
144
150
- url_prefix : str
145
+ url_prefix : str = ""
151
146
"""The URL prefix where IDOM resources will be served from"""
152
147
153
148
154
- def _setup_options (options : Options | None ) -> Options :
155
- return {
156
- "url_prefix" : "" ,
157
- "cors" : False ,
158
- "serve_static_files" : True ,
159
- "redirect_root_to_index" : True ,
160
- ** (options or {}), # type: ignore
161
- }
162
-
163
-
164
149
def _setup_common_routes (blueprint : Blueprint , options : Options ) -> None :
165
- cors_options = options [ " cors" ]
150
+ cors_options = options . cors
166
151
if cors_options : # pragma: no cover
167
152
cors_params = cors_options if isinstance (cors_options , dict ) else {}
168
153
CORS (blueprint , ** cors_params )
169
154
170
- if options [ " serve_static_files" ] :
155
+ if options . serve_static_files :
171
156
172
157
@blueprint .route ("/client/<path:path>" )
173
158
def send_client_dir (path : str ) -> Any :
@@ -177,7 +162,7 @@ def send_client_dir(path: str) -> Any:
177
162
def send_modules_dir (path : str ) -> Any :
178
163
return send_from_directory (str (IDOM_WEB_MODULES_DIR .current ), path )
179
164
180
- if options [ "redirect_root_to_index" ] :
165
+ if options . redirect_root :
181
166
182
167
@blueprint .route ("/" )
183
168
def redirect_to_index () -> Any :
@@ -195,7 +180,7 @@ def _setup_single_view_dispatcher_route(
195
180
) -> None :
196
181
sockets = Sockets (app )
197
182
198
- @sockets .route (_join_url_paths (options [ " url_prefix" ] , "/stream" )) # type: ignore
183
+ @sockets .route (_join_url_paths (options . url_prefix , "/stream" )) # type: ignore
199
184
def model_stream (ws : WebSocket ) -> None :
200
185
def send (value : Any ) -> None :
201
186
ws .send (json .dumps (value ))
0 commit comments