File tree Expand file tree Collapse file tree 7 files changed +71
-5
lines changed Expand file tree Collapse file tree 7 files changed +71
-5
lines changed Original file line number Diff line number Diff line change
1
+ 'Test harness code'
2
+
3
+ from django_plotly_dash import DjangoDash
4
+ from django .utils .module_loading import import_string
5
+
6
+ def stateless_app_loader (app_name ):
7
+
8
+ # Load a stateless app
9
+ return import_string ("demo.scaffold." + app_name )
10
+
11
+ demo_app = DjangoDash (name = "name_of_demo_app" )
12
+
Original file line number Diff line number Diff line change 141
141
"cache_arguments" : True , # True for cache, False for session-based argument propagation
142
142
143
143
#"serve_locally" : True, # True to serve assets locally, False to use their unadulterated urls (eg a CDN)
144
+
145
+ "stateless_loader" : "demo.scaffold.stateless_app_loader" ,
144
146
}
145
147
146
148
# Static files (CSS, JavaScript, Images)
Original file line number Diff line number Diff line change @@ -87,3 +87,18 @@ def test_app_callbacks():
87
87
assert liveOut
88
88
89
89
# TODO need something to trigger callbacks
90
+
91
+ def test_stateless_lookup ():
92
+ 'Test side loading of stateless apps'
93
+
94
+ from django_plotly_dash .util import stateless_app_lookup_hook
95
+ lh_hook = stateless_app_lookup_hook ()
96
+
97
+ with pytest .raises (ImportError ):
98
+ lh_hook ("not a real app name" )
99
+
100
+ demo_app = lh_hook ('demo_app' )
101
+
102
+ assert demo_app is not None
103
+ assert demo_app ._uid == 'name_of_demo_app'
104
+
Original file line number Diff line number Diff line change 41
41
42
42
from .util import static_asset_path
43
43
from .util import serve_locally as serve_locally_setting
44
+ from .util import stateless_app_lookup_hook
44
45
45
46
uid_counter = 0
46
47
47
48
usable_apps = {}
48
49
50
+ _stateless_app_lookup_func = None
51
+
49
52
def add_usable_app (name , app ):
50
53
'Add app to local registry by name'
51
54
name = slugify (name )
@@ -62,12 +65,24 @@ def get_local_stateless_by_name(name):
62
65
Locate a registered dash app by name, and return a DjangoDash instance encapsulating the app.
63
66
'''
64
67
name = slugify (name )
65
- # TODO wrap this in raising a 404 if not found
66
- try :
67
- return usable_apps [name ]
68
- except :
68
+
69
+ sa = usable_apps .get (name , None )
70
+
71
+ if not sa :
72
+
73
+ global _stateless_app_lookup_func # pylint: disable=global-statement
74
+
75
+ if _stateless_app_lookup_func is None :
76
+ _stateless_app_lookup_func = stateless_app_lookup_hook ()
77
+
78
+ sa = stateless_app_lookup_func (name )
79
+
80
+ if not sa :
81
+ # TODO wrap this in raising a 404 if not found
69
82
raise KeyError ("Unable to find stateless DjangoApp called %s" % name )
70
83
84
+ return sa
85
+
71
86
class Holder :
72
87
'Helper class for holding configuration options'
73
88
def __init__ (self ):
Original file line number Diff line number Diff line change @@ -253,6 +253,16 @@ def test_argument_settings(settings, client):
253
253
assert store_initial_arguments (client , None ) is None
254
254
assert get_initial_arguments (client , None ) is None
255
255
256
+ def test_stateless_lookup_noop ():
257
+ 'Test no-op stateless lookup'
258
+
259
+ from django_plotly_dash .util import stateless_app_lookup_hook
260
+ lh_hook = stateless_app_lookup_hook ()
261
+
262
+ assert lh_hook is not None
263
+ with pytest .raises (ImportError ):
264
+ lh_hook ("not an app" )
265
+
256
266
def test_middleware_artifacts ():
257
267
'Import and vaguely exercise middleware objects'
258
268
Original file line number Diff line number Diff line change 27
27
28
28
from django .conf import settings
29
29
from django .core .cache import cache
30
+ from django .utils .module_loading import import_string
30
31
31
32
def _get_settings ():
32
33
try :
@@ -117,3 +118,14 @@ def static_path(relative_path):
117
118
except :
118
119
static_url = '/static/'
119
120
return "%s%s" % (static_url , relative_path )
121
+
122
+ def stateless_app_lookup_hook ():
123
+ 'Return a function that performs lookup for aa stateless app, given its name, or returns None'
124
+
125
+ func_name = _get_settings ().get ('stateless_loader' , None )
126
+ if func_name :
127
+ func = import_string (func_name )
128
+ return func
129
+
130
+ # Default is no additional lookup
131
+ return lambda _ : None
Original file line number Diff line number Diff line change 23
23
24
24
'''
25
25
26
- __version__ = "0.9.13 "
26
+ __version__ = "0.9.14 "
You can’t perform that action at this time.
0 commit comments