-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
Copy pathdash.py
1292 lines (1121 loc) · 48.7 KB
/
dash.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
from __future__ import print_function
import itertools
import os
import random
import sys
import collections
import importlib
import json
import pkgutil
import threading
import warnings
import re
import logging
from functools import wraps
import plotly
import dash_renderer
import flask
from flask import Flask, Response
from flask_compress import Compress
from .dependencies import Event, Input, Output, State
from .resources import Scripts, Css
from .development.base_component import Component
from . import exceptions
from ._utils import AttributeDict as _AttributeDict
from ._utils import interpolate_str as _interpolate
from ._utils import format_tag as _format_tag
from ._utils import generate_hash as _generate_hash
from . import _watch
from ._utils import get_asset_path as _get_asset_path
from . import _configs
_default_index = '''<!DOCTYPE html>
<html>
<head>
{%metas%}
<title>{%title%}</title>
{%favicon%}
{%css%}
</head>
<body>
{%app_entry%}
<footer>
{%config%}
{%scripts%}
</footer>
</body>
</html>'''
_app_entry = '''
<div id="react-entry-point">
<div class="_dash-loading">
Loading...
</div>
</div>
'''
_re_index_entry = re.compile(r'{%app_entry%}')
_re_index_config = re.compile(r'{%config%}')
_re_index_scripts = re.compile(r'{%scripts%}')
_re_index_entry_id = re.compile(r'id="react-entry-point"')
_re_index_config_id = re.compile(r'id="_dash-config"')
_re_index_scripts_id = re.compile(r'src=".*dash[-_]renderer.*"')
# pylint: disable=too-many-instance-attributes
# pylint: disable=too-many-arguments, too-many-locals
class Dash(object):
def __init__(
self,
name='__main__',
server=None,
static_folder='static',
assets_folder=None,
assets_url_path='/assets',
assets_ignore='',
include_assets_files=True,
url_base_pathname=None,
assets_external_path=None,
requests_pathname_prefix=None,
routes_pathname_prefix=None,
compress=True,
meta_tags=None,
index_string=_default_index,
external_scripts=None,
external_stylesheets=None,
suppress_callback_exceptions=None,
components_cache_max_age=None,
**kwargs):
# pylint-disable: too-many-instance-attributes
if 'csrf_protect' in kwargs:
warnings.warn('''
`csrf_protect` is no longer used,
CSRF protection has been removed as it is no longer
necessary.
See https://github.com/plotly/dash/issues/141 for details.
''', DeprecationWarning)
name = name if server is None else server.name
self._assets_folder = assets_folder or os.path.join(
flask.helpers.get_root_path(name), 'assets'
)
self._assets_url_path = assets_url_path
# allow users to supply their own flask server
self.server = server or Flask(name, static_folder=static_folder)
if 'assets' not in self.server.blueprints:
self.server.register_blueprint(
flask.Blueprint('assets', 'assets',
static_folder=self._assets_folder,
static_url_path=assets_url_path))
env_configs = _configs.env_configs()
url_base_pathname, routes_pathname_prefix, requests_pathname_prefix = \
_configs.pathname_configs(
url_base_pathname,
routes_pathname_prefix,
requests_pathname_prefix,
environ_configs=env_configs)
self.url_base_pathname = url_base_pathname
self.config = _AttributeDict({
'suppress_callback_exceptions': _configs.get_config(
'suppress_callback_exceptions',
suppress_callback_exceptions, env_configs, False
),
'routes_pathname_prefix': routes_pathname_prefix,
'requests_pathname_prefix': requests_pathname_prefix,
'include_assets_files': _configs.get_config(
'include_assets_files',
include_assets_files,
env_configs,
True),
'assets_external_path': _configs.get_config(
'assets_external_path', assets_external_path, env_configs, ''),
'components_cache_max_age': int(_configs.get_config(
'components_cache_max_age', components_cache_max_age,
env_configs, 2678400))
})
# list of dependencies
self.callback_map = {}
self._index_string = ''
self.index_string = index_string
self._meta_tags = meta_tags or []
self._favicon = None
if compress:
# gzip
Compress(self.server)
@self.server.errorhandler(exceptions.PreventUpdate)
def _handle_error(error):
"""Handle a halted callback and return an empty 204 response"""
print(error, file=sys.stderr)
return ('', 204)
# static files from the packages
self.css = Css()
self.scripts = Scripts()
self._external_scripts = external_scripts or []
self._external_stylesheets = external_stylesheets or []
self.assets_ignore = assets_ignore
self.registered_paths = collections.defaultdict(set)
# urls
self.routes = []
self._add_url(
'{}_dash-layout'.format(self.config['routes_pathname_prefix']),
self.serve_layout)
self._add_url(
'{}_dash-dependencies'.format(
self.config['routes_pathname_prefix']),
self.dependencies)
self._add_url(
'{}_dash-update-component'.format(
self.config['routes_pathname_prefix']),
self.dispatch,
['POST'])
self._add_url((
'{}_dash-component-suites'
'/<string:package_name>'
'/<path:path_in_package_dist>').format(
self.config['routes_pathname_prefix']),
self.serve_component_suites)
self._add_url(
'{}_dash-routes'.format(self.config['routes_pathname_prefix']),
self.serve_routes)
self._add_url(
self.config['routes_pathname_prefix'],
self.index)
self._add_url(
'{}_reload-hash'.format(self.config['routes_pathname_prefix']),
self.serve_reload_hash)
# catch-all for front-end routes, used by dcc.Location
self._add_url(
'{}<path:path>'.format(self.config['routes_pathname_prefix']),
self.index)
self._add_url(
'{}_favicon.ico'.format(self.config['routes_pathname_prefix']),
self._serve_default_favicon)
self.server.before_first_request(self._setup_server)
self._layout = None
self._cached_layout = None
self._dev_tools = _AttributeDict({
'serve_dev_bundles': False,
'hot_reload': False,
'hot_reload_interval': 3000,
'hot_reload_watch_interval': 0.5,
'hot_reload_max_retry': 8
})
# add a handler for components suites errors to return 404
self.server.errorhandler(exceptions.InvalidResourceError)(
self._invalid_resources_handler)
self._assets_files = []
# hot reload
self._reload_hash = None
self._hard_reload = False
self._lock = threading.RLock()
self._watch_thread = None
self._changed_assets = []
self.logger = logging.getLogger(name)
self.logger.addHandler(logging.StreamHandler(stream=sys.stdout))
def _add_url(self, name, view_func, methods=('GET',)):
self.server.add_url_rule(
name,
view_func=view_func,
endpoint=name,
methods=list(methods))
# record the url in Dash.routes so that it can be accessed later
# e.g. for adding authentication with flask_login
self.routes.append(name)
@property
def layout(self):
return self._layout
def _layout_value(self):
if isinstance(self._layout, collections.Callable):
self._cached_layout = self._layout()
else:
self._cached_layout = self._layout
return self._cached_layout
@layout.setter
def layout(self, value):
if (not isinstance(value, Component) and
not isinstance(value, collections.Callable)):
raise exceptions.NoLayoutException(
''
'Layout must be a dash component '
'or a function that returns '
'a dash component.')
self._layout = value
layout_value = self._layout_value()
# pylint: disable=protected-access
self.css._update_layout(layout_value)
self.scripts._update_layout(layout_value)
@property
def index_string(self):
return self._index_string
@index_string.setter
def index_string(self, value):
checks = (
(_re_index_entry.search(value), 'app_entry'),
(_re_index_config.search(value), 'config',),
(_re_index_scripts.search(value), 'scripts'),
)
missing = [missing for check, missing in checks if not check]
if missing:
raise exceptions.InvalidIndexException(
'Did you forget to include {} in your index string ?'.format(
', '.join('{%' + x + '%}' for x in missing)
)
)
self._index_string = value
def serve_layout(self):
layout = self._layout_value()
# TODO - Set browser cache limit - pass hash into frontend
return flask.Response(
json.dumps(layout,
cls=plotly.utils.PlotlyJSONEncoder),
mimetype='application/json'
)
def _config(self):
config = {
'url_base_pathname': self.url_base_pathname,
'requests_pathname_prefix': self.config['requests_pathname_prefix']
}
if self._dev_tools.hot_reload:
config['hot_reload'] = {
'interval': self._dev_tools.hot_reload_interval,
'max_retry': self._dev_tools.hot_reload_max_retry
}
return config
def serve_reload_hash(self):
hard = self._hard_reload
changed = self._changed_assets
self._lock.acquire()
self._hard_reload = False
self._changed_assets = []
self._lock.release()
return flask.jsonify({
'reloadHash': self._reload_hash,
'hard': hard,
'packages': list(self.registered_paths.keys()),
'files': list(changed)
})
def serve_routes(self):
return flask.Response(
json.dumps(self.routes,
cls=plotly.utils.PlotlyJSONEncoder),
mimetype='application/json'
)
def _collect_and_register_resources(self, resources):
# now needs the app context.
# template in the necessary component suite JS bundles
# add the version number of the package as a query parameter
# for cache busting
def _relative_url_path(relative_package_path='', namespace=''):
module_path = os.path.join(
os.path.dirname(sys.modules[namespace].__file__),
relative_package_path)
modified = int(os.stat(module_path).st_mtime)
return '{}_dash-component-suites/{}/{}?v={}&m={}'.format(
self.config['requests_pathname_prefix'],
namespace,
relative_package_path,
importlib.import_module(namespace).__version__,
modified
)
srcs = []
for resource in resources:
is_dynamic_resource = resource.get('dynamic', False)
if 'relative_package_path' in resource:
paths = resource['relative_package_path']
paths = [paths] if isinstance(paths, str) else paths
for rel_path in paths:
self.registered_paths[resource['namespace']]\
.add(rel_path)
if not is_dynamic_resource:
srcs.append(_relative_url_path(
relative_package_path=rel_path,
namespace=resource['namespace']
))
elif 'external_url' in resource:
if isinstance(resource['external_url'], str):
srcs.append(resource['external_url'])
else:
for url in resource['external_url']:
srcs.append(url)
elif 'absolute_path' in resource:
raise Exception(
'Serving files from absolute_path isn\'t supported yet'
)
elif 'asset_path' in resource:
static_url = self.get_asset_url(resource['asset_path'])
# Add a bust query param
static_url += '?m={}'.format(resource['ts'])
srcs.append(static_url)
return srcs
def _generate_css_dist_html(self):
links = self._external_stylesheets + \
self._collect_and_register_resources(self.css.get_all_css())
return '\n'.join([
_format_tag('link', link, opened=True)
if isinstance(link, dict)
else '<link rel="stylesheet" href="{}">'.format(link)
for link in links
])
def _generate_scripts_html(self):
# Dash renderer has dependencies like React which need to be rendered
# before every other script. However, the dash renderer bundle
# itself needs to be rendered after all of the component's
# scripts have rendered.
# The rest of the scripts can just be loaded after React but before
# dash renderer.
# pylint: disable=protected-access
srcs = self._collect_and_register_resources(
self.scripts._resources._filter_resources(
dash_renderer._js_dist_dependencies,
dev_bundles=self._dev_tools.serve_dev_bundles
)) + self._external_scripts + self._collect_and_register_resources(
self.scripts.get_all_scripts(
dev_bundles=self._dev_tools.serve_dev_bundles) +
self.scripts._resources._filter_resources(
dash_renderer._js_dist,
dev_bundles=self._dev_tools.serve_dev_bundles
))
return '\n'.join([
_format_tag('script', src)
if isinstance(src, dict)
else '<script src="{}"></script>'.format(src)
for src in srcs
])
def _generate_config_html(self):
return (
'<script id="_dash-config" type="application/json">'
'{}'
'</script>'
).format(json.dumps(self._config()))
def _generate_meta_html(self):
has_ie_compat = any(
x.get('http-equiv', '') == 'X-UA-Compatible'
for x in self._meta_tags)
has_charset = any('charset' in x for x in self._meta_tags)
tags = []
if not has_ie_compat:
tags.append(
'<meta http-equiv="X-UA-Compatible" content="IE=edge">'
)
if not has_charset:
tags.append('<meta charset="UTF-8">')
tags = tags + [
_format_tag('meta', x, opened=True) for x in self._meta_tags
]
return '\n '.join(tags)
# Serve the JS bundles for each package
def serve_component_suites(self, package_name, path_in_package_dist):
if package_name not in self.registered_paths:
raise exceptions.DependencyException(
'Error loading dependency.\n'
'"{}" is not a registered library.\n'
'Registered libraries are: {}'
.format(package_name, list(self.registered_paths.keys())))
elif path_in_package_dist not in self.registered_paths[package_name]:
raise exceptions.DependencyException(
'"{}" is registered but the path requested is not valid.\n'
'The path requested: "{}"\n'
'List of registered paths: {}'
.format(
package_name,
path_in_package_dist,
self.registered_paths
)
)
mimetype = ({
'js': 'application/JavaScript',
'css': 'text/css',
'map': 'application/json'
})[path_in_package_dist.split('.')[-1]]
headers = {
'Cache-Control': 'public, max-age={}'.format(
self.config.components_cache_max_age)
}
return Response(
pkgutil.get_data(package_name, path_in_package_dist),
mimetype=mimetype,
headers=headers
)
def index(self, *args, **kwargs): # pylint: disable=unused-argument
scripts = self._generate_scripts_html()
css = self._generate_css_dist_html()
config = self._generate_config_html()
metas = self._generate_meta_html()
title = getattr(self, 'title', 'Dash')
if self._favicon:
favicon_mod_time = os.path.getmtime(
os.path.join(self._assets_folder, self._favicon))
favicon_url = self.get_asset_url(self._favicon) + '?m={}'.format(
favicon_mod_time
)
else:
favicon_url = '{}_favicon.ico'.format(
self.config.requests_pathname_prefix)
favicon = _format_tag('link', {
'rel': 'icon',
'type': 'image/x-icon',
'href': favicon_url
}, opened=True)
index = self.interpolate_index(
metas=metas, title=title, css=css, config=config,
scripts=scripts, app_entry=_app_entry, favicon=favicon)
checks = (
(_re_index_entry_id.search(index), '#react-entry-point'),
(_re_index_config_id.search(index), '#_dash-configs'),
(_re_index_scripts_id.search(index), 'dash-renderer'),
)
missing = [missing for check, missing in checks if not check]
if missing:
plural = 's' if len(missing) > 1 else ''
raise exceptions.InvalidIndexException(
'Missing element{pl} {ids} in index.'.format(
ids=', '.join(missing),
pl=plural
)
)
return index
def interpolate_index(self,
metas='', title='', css='', config='',
scripts='', app_entry='', favicon=''):
"""
Called to create the initial HTML string that is loaded on page.
Override this method to provide you own custom HTML.
:Example:
class MyDash(dash.Dash):
def interpolate_index(self, **kwargs):
return '''
<!DOCTYPE html>
<html>
<head>
<title>My App</title>
</head>
<body>
<div id="custom-header">My custom header</div>
{app_entry}
{config}
{scripts}
<div id="custom-footer">My custom footer</div>
</body>
</html>
'''.format(
app_entry=kwargs.get('app_entry'),
config=kwargs.get('config'),
scripts=kwargs.get('scripts'))
:param metas: Collected & formatted meta tags.
:param title: The title of the app.
:param css: Collected & formatted css dependencies as <link> tags.
:param config: Configs needed by dash-renderer.
:param scripts: Collected & formatted scripts tags.
:param app_entry: Where the app will render.
:param favicon: A favicon <link> tag if found in assets folder.
:return: The interpolated HTML string for the index.
"""
return _interpolate(self.index_string,
metas=metas,
title=title,
css=css,
config=config,
scripts=scripts,
favicon=favicon,
app_entry=app_entry)
def dependencies(self):
return flask.jsonify([
{
'output': {
'id': k.split('.')[0],
'property': k.split('.')[1]
},
'inputs': v['inputs'],
'state': v['state'],
'events': v['events']
} for k, v in self.callback_map.items()
])
# pylint: disable=unused-argument, no-self-use
def react(self, *args, **kwargs):
raise exceptions.DashException(
'Yo! `react` is no longer used. \n'
'Use `callback` instead. `callback` has a new syntax too, '
'so make sure to call `help(app.callback)` to learn more.')
def _validate_callback(self, output, inputs, state, events):
# pylint: disable=too-many-branches
layout = self._cached_layout or self._layout_value()
if (layout is None and
not self.config.first('suppress_callback_exceptions',
'supress_callback_exceptions')):
# Without a layout, we can't do validation on the IDs and
# properties of the elements in the callback.
raise exceptions.LayoutIsNotDefined('''
Attempting to assign a callback to the application but
the `layout` property has not been assigned.
Assign the `layout` property before assigning callbacks.
Alternatively, suppress this warning by setting
`app.config['suppress_callback_exceptions']=True`
'''.replace(' ', ''))
for args, obj, name in [([output], Output, 'Output'),
(inputs, Input, 'Input'),
(state, State, 'State'),
(events, Event, 'Event')]:
if not isinstance(args, list):
raise exceptions.IncorrectTypeException(
'The {} argument `{}` is '
'not a list of `dash.dependencies.{}`s.'.format(
name.lower(), str(args), name
))
for arg in args:
if not isinstance(arg, obj):
raise exceptions.IncorrectTypeException(
'The {} argument `{}` is '
'not of type `dash.{}`.'.format(
name.lower(), str(arg), name
))
if (not self.config.first('suppress_callback_exceptions',
'supress_callback_exceptions') and
arg.component_id not in layout and
arg.component_id != getattr(layout, 'id', None)):
raise exceptions.NonExistantIdException('''
Attempting to assign a callback to the
component with the id "{}" but no
components with id "{}" exist in the
app\'s layout.\n\n
Here is a list of IDs in layout:\n{}\n\n
If you are assigning callbacks to components
that are generated by other callbacks
(and therefore not in the initial layout), then
you can suppress this exception by setting
`app.config['suppress_callback_exceptions']=True`.
'''.format(
arg.component_id,
arg.component_id,
list(layout.keys()) + (
[] if not hasattr(layout, 'id') else
[layout.id]
)
).replace(' ', ''))
if not self.config.first('suppress_callback_exceptions',
'supress_callback_exceptions'):
if getattr(layout, 'id', None) == arg.component_id:
component = layout
else:
component = layout[arg.component_id]
if (hasattr(arg, 'component_property') and
arg.component_property not in
component.available_properties and not
any(arg.component_property.startswith(w) for w in
component.available_wildcard_properties)):
raise exceptions.NonExistantPropException('''
Attempting to assign a callback with
the property "{}" but the component
"{}" doesn't have "{}" as a property.\n
Here is a list of the available properties in "{}":
{}
'''.format(
arg.component_property,
arg.component_id,
arg.component_property,
arg.component_id,
component.available_properties).replace(
' ', ''))
if (hasattr(arg, 'component_event') and
arg.component_event not in
component.available_events):
raise exceptions.NonExistantEventException('''
Attempting to assign a callback with
the event "{}" but the component
"{}" doesn't have "{}" as an event.\n
Here is a list of the available events in "{}":
{}
'''.format(
arg.component_event,
arg.component_id,
arg.component_event,
arg.component_id,
component.available_events).replace(' ', ''))
if state and not events and not inputs:
raise exceptions.MissingEventsException('''
This callback has {} `State` {}
but no `Input` elements or `Event` elements.\n
Without `Input` or `Event` elements, this callback
will never get called.\n
(Subscribing to input components will cause the
callback to be called whenever their values
change and subscribing to an event will cause the
callback to be called whenever the event is fired.)
'''.format(
len(state),
'elements' if len(state) > 1 else 'element'
).replace(' ', ''))
if '.' in output.component_id:
raise exceptions.IDsCantContainPeriods('''The Output element
`{}` contains a period in its ID.
Periods are not allowed in IDs right now.'''.format(
output.component_id
))
callback_id = '{}.{}'.format(
output.component_id, output.component_property)
if callback_id in self.callback_map:
raise exceptions.CantHaveMultipleOutputs('''
You have already assigned a callback to the output
with ID "{}" and property "{}". An output can only have
a single callback function. Try combining your inputs and
callback functions together into one function.
'''.format(
output.component_id,
output.component_property).replace(' ', ''))
def _validate_callback_output(self, output_value, output):
valid = [str, dict, int, float, type(None), Component]
def _raise_invalid(bad_val, outer_val, bad_type, path, index=None,
toplevel=False):
outer_id = "(id={:s})".format(outer_val.id) \
if getattr(outer_val, 'id', False) else ''
outer_type = type(outer_val).__name__
raise exceptions.InvalidCallbackReturnValue('''
The callback for property `{property:s}` of component `{id:s}`
returned a {object:s} having type `{type:s}`
which is not JSON serializable.
{location_header:s}{location:s}
and has string representation
`{bad_val}`
In general, Dash properties can only be
dash components, strings, dictionaries, numbers, None,
or lists of those.
'''.format(
property=output.component_property,
id=output.component_id,
object='tree with one value' if not toplevel else 'value',
type=bad_type,
location_header=(
'The value in question is located at'
if not toplevel else
'''The value in question is either the only value returned,
or is in the top level of the returned list,'''
),
location=(
"\n" +
("[{:d}] {:s} {:s}".format(index, outer_type, outer_id)
if index is not None
else ('[*] ' + outer_type + ' ' + outer_id))
+ "\n" + path + "\n"
) if not toplevel else '',
bad_val=bad_val).replace(' ', ''))
def _value_is_valid(val):
return (
# pylint: disable=unused-variable
any([isinstance(val, x) for x in valid]) or
type(val).__name__ == 'unicode'
)
def _validate_value(val, index=None):
# val is a Component
if isinstance(val, Component):
for p, j in val.traverse_with_paths():
# check each component value in the tree
if not _value_is_valid(j):
_raise_invalid(
bad_val=j,
outer_val=val,
bad_type=type(j).__name__,
path=p,
index=index
)
# Children that are not of type Component or
# list/tuple not returned by traverse
child = getattr(j, 'children', None)
if not isinstance(child, (tuple,
collections.MutableSequence)):
if child and not _value_is_valid(child):
_raise_invalid(
bad_val=child,
outer_val=val,
bad_type=type(child).__name__,
path=p + "\n" + "[*] " + type(child).__name__,
index=index
)
# Also check the child of val, as it will not be returned
child = getattr(val, 'children', None)
if not isinstance(child, (tuple, collections.MutableSequence)):
if child and not _value_is_valid(child):
_raise_invalid(
bad_val=child,
outer_val=val,
bad_type=type(child).__name__,
path=type(child).__name__,
index=index
)
# val is not a Component, but is at the top level of tree
else:
if not _value_is_valid(val):
_raise_invalid(
bad_val=val,
outer_val=type(val).__name__,
bad_type=type(val).__name__,
path='',
index=index,
toplevel=True
)
if isinstance(output_value, list):
for i, val in enumerate(output_value):
_validate_value(val, index=i)
else:
_validate_value(output_value)
# TODO - Update nomenclature.
# "Parents" and "Children" should refer to the DOM tree
# and not the dependency tree.
# The dependency tree should use the nomenclature
# "observer" and "controller".
# "observers" listen for changes from their "controllers". For example,
# if a graph depends on a dropdown, the graph is the "observer" and the
# dropdown is a "controller". In this case the graph's "dependency" is
# the dropdown.
# TODO - Check this map for recursive or other ill-defined non-tree
# relationships
# pylint: disable=dangerous-default-value
def callback(self, output, inputs=[], state=[], events=[]):
self._validate_callback(output, inputs, state, events)
callback_id = '{}.{}'.format(
output.component_id, output.component_property
)
self.callback_map[callback_id] = {
'inputs': [
{'id': c.component_id, 'property': c.component_property}
for c in inputs
],
'state': [
{'id': c.component_id, 'property': c.component_property}
for c in state
],
'events': [
{'id': c.component_id, 'event': c.component_event}
for c in events
]
}
def wrap_func(func):
@wraps(func)
def add_context(*args, **kwargs):
output_value = func(*args, **kwargs)
response = {
'response': {
'props': {
output.component_property: output_value
}
}
}
try:
jsonResponse = json.dumps(
response,
cls=plotly.utils.PlotlyJSONEncoder
)
except TypeError:
self._validate_callback_output(output_value, output)
raise exceptions.InvalidCallbackReturnValue('''
The callback for property `{property:s}`
of component `{id:s}` returned a value
which is not JSON serializable.
In general, Dash properties can only be
dash components, strings, dictionaries, numbers, None,
or lists of those.
'''.format(property=output.component_property,
id=output.component_id))
return flask.Response(
jsonResponse,
mimetype='application/json'
)
self.callback_map[callback_id]['callback'] = add_context
return add_context
return wrap_func
def dispatch(self):
body = flask.request.get_json()
inputs = body.get('inputs', [])
state = body.get('state', [])
output = body['output']
target_id = '{}.{}'.format(output['id'], output['property'])
args = []
for component_registration in self.callback_map[target_id]['inputs']:
args.append([
c.get('value', None) for c in inputs if
c['property'] == component_registration['property'] and
c['id'] == component_registration['id']
][0])
for component_registration in self.callback_map[target_id]['state']:
args.append([
c.get('value', None) for c in state if
c['property'] == component_registration['property'] and
c['id'] == component_registration['id']
][0])
return self.callback_map[target_id]['callback'](*args)
def _validate_layout(self):
if self.layout is None:
raise exceptions.NoLayoutException(
''
'The layout was `None` '
'at the time that `run_server` was called. '
'Make sure to set the `layout` attribute of your application '
'before running the server.')
to_validate = self._layout_value()
layout_id = getattr(self.layout, 'id', None)
component_ids = {layout_id} if layout_id else set()
for component in to_validate.traverse():
component_id = getattr(component, 'id', None)
if component_id and component_id in component_ids:
raise exceptions.DuplicateIdError(
'Duplicate component id found'
' in the initial layout: `{}`'.format(component_id))
component_ids.add(component_id)
def _setup_server(self):
if self.config.include_assets_files:
self._walk_assets_directory()
self._validate_layout()
self._generate_scripts_html()
self._generate_css_dist_html()
def _add_assets_resource(self, url_path, file_path):
res = {'asset_path': url_path, 'filepath': file_path}