18
18
import base64
19
19
import traceback
20
20
from urllib .parse import urlparse
21
- from typing import Any , Callable , Dict , Optional , Union , Sequence , cast
21
+ from typing import Any , Callable , Dict , Optional , Union , Sequence , cast , Literal
22
22
23
23
import flask
24
24
25
+ from flask .typing import RouteCallable
25
26
from importlib_metadata import version as _get_distribution_version
26
27
27
28
from dash import dcc
@@ -622,7 +623,7 @@ def _setup_hooks(self):
622
623
if self ._hooks .get_hooks ("error" ):
623
624
self ._on_error = self ._hooks .HookErrorHandler (self ._on_error )
624
625
625
- def init_app (self , app = None , ** kwargs ):
626
+ def init_app (self , app : Optional [ flask . Flask ] = None , ** kwargs ) -> None :
626
627
"""Initialize the parts of Dash that require a flask app."""
627
628
628
629
config = self .config
@@ -694,7 +695,7 @@ def _handle_error(_):
694
695
695
696
self ._setup_plotlyjs ()
696
697
697
- def _add_url (self , name , view_func , methods = ("GET" ,)):
698
+ def _add_url (self , name : str , view_func : RouteCallable , methods = ("GET" ,)) -> None :
698
699
full_name = self .config .routes_pathname_prefix + name
699
700
700
701
self .server .add_url_rule (
@@ -748,11 +749,11 @@ def _setup_plotlyjs(self):
748
749
self ._plotlyjs_url = url
749
750
750
751
@property
751
- def layout (self ):
752
+ def layout (self ) -> Any :
752
753
return self ._layout
753
754
754
755
@layout .setter
755
- def layout (self , value ):
756
+ def layout (self , value : Any ):
756
757
_validate .validate_layout_type (value )
757
758
self ._layout_is_function = callable (value )
758
759
self ._layout = value
@@ -782,11 +783,11 @@ def _layout_value(self):
782
783
return layout
783
784
784
785
@property
785
- def index_string (self ):
786
+ def index_string (self ) -> str :
786
787
return self ._index_string
787
788
788
789
@index_string .setter
789
- def index_string (self , value ) :
790
+ def index_string (self , value : str ) -> None :
790
791
checks = (_re_index_entry , _re_index_config , _re_index_scripts )
791
792
_validate .validate_index ("index string" , checks , value )
792
793
self ._index_string = value
@@ -861,7 +862,7 @@ def serve_reload_hash(self):
861
862
}
862
863
)
863
864
864
- def get_dist (self , libraries ) :
865
+ def get_dist (self , libraries : Sequence [ str ]) -> list :
865
866
dists = []
866
867
for dist_type in ("_js_dist" , "_css_dist" ):
867
868
resources = ComponentRegistry .get_resources (dist_type , libraries )
@@ -963,7 +964,7 @@ def _generate_css_dist_html(self):
963
964
]
964
965
)
965
966
966
- def _generate_scripts_html (self ):
967
+ def _generate_scripts_html (self ) -> str :
967
968
# Dash renderer has dependencies like React which need to be rendered
968
969
# before every other script. However, the dash renderer bundle
969
970
# itself needs to be rendered after all of the component's
@@ -1020,10 +1021,10 @@ def _generate_scripts_html(self):
1020
1021
+ [f"<script>{ src } </script>" for src in self ._inline_scripts ]
1021
1022
)
1022
1023
1023
- def _generate_config_html (self ):
1024
+ def _generate_config_html (self ) -> str :
1024
1025
return f'<script id="_dash-config" type="application/json">{ to_json (self ._config ())} </script>'
1025
1026
1026
- def _generate_renderer (self ):
1027
+ def _generate_renderer (self ) -> str :
1027
1028
return f'<script id="_dash-renderer" type="application/javascript">{ self .renderer } </script>'
1028
1029
1029
1030
def _generate_meta (self ):
@@ -1545,7 +1546,7 @@ def _serve_default_favicon():
1545
1546
pkgutil .get_data ("dash" , "favicon.ico" ), content_type = "image/x-icon"
1546
1547
)
1547
1548
1548
- def csp_hashes (self , hash_algorithm = "sha256" ):
1549
+ def csp_hashes (self , hash_algorithm = "sha256" ) -> Sequence [ str ] :
1549
1550
"""Calculates CSP hashes (sha + base64) of all inline scripts, such that
1550
1551
one of the biggest benefits of CSP (disallowing general inline scripts)
1551
1552
can be utilized together with Dash clientside callbacks (inline scripts).
@@ -1584,7 +1585,7 @@ def _hash(script):
1584
1585
for script in (self ._inline_scripts + [self .renderer ])
1585
1586
]
1586
1587
1587
- def get_asset_url (self , path ) :
1588
+ def get_asset_url (self , path : str ) -> str :
1588
1589
"""
1589
1590
Return the URL for the provided `path` in the assets directory.
1590
1591
@@ -1655,7 +1656,7 @@ def display_content(path):
1655
1656
self .config .requests_pathname_prefix , path
1656
1657
)
1657
1658
1658
- def strip_relative_path (self , path ) :
1659
+ def strip_relative_path (self , path : str ) -> Union [ str , None ] :
1659
1660
"""
1660
1661
Return a path with `requests_pathname_prefix` and leading and trailing
1661
1662
slashes stripped from it. Also, if None is passed in, None is returned.
@@ -1707,7 +1708,9 @@ def display_content(path):
1707
1708
)
1708
1709
1709
1710
@staticmethod
1710
- def add_startup_route (name , view_func , methods ):
1711
+ def add_startup_route (
1712
+ name : str , view_func : RouteCallable , methods : Sequence [Literal ["POST" , "GET" ]]
1713
+ ) -> None :
1711
1714
"""
1712
1715
Add a route to the app to be initialized at the end of Dash initialization.
1713
1716
Use this if the package requires a route to be added to the app, and you will not need to worry about at what point to add it.
@@ -1731,7 +1734,7 @@ def add_startup_route(name, view_func, methods):
1731
1734
1732
1735
Dash .STARTUP_ROUTES .append ((name , view_func , methods ))
1733
1736
1734
- def setup_startup_routes (self ):
1737
+ def setup_startup_routes (self ) -> None :
1735
1738
"""
1736
1739
Initialize the startup routes stored in STARTUP_ROUTES.
1737
1740
"""
@@ -1774,18 +1777,18 @@ def _setup_dev_tools(self, **kwargs):
1774
1777
1775
1778
def enable_dev_tools (
1776
1779
self ,
1777
- debug = None ,
1778
- dev_tools_ui = None ,
1779
- dev_tools_props_check = None ,
1780
- dev_tools_serve_dev_bundles = None ,
1781
- dev_tools_hot_reload = None ,
1782
- dev_tools_hot_reload_interval = None ,
1783
- dev_tools_hot_reload_watch_interval = None ,
1784
- dev_tools_hot_reload_max_retry = None ,
1785
- dev_tools_silence_routes_logging = None ,
1786
- dev_tools_disable_version_check = None ,
1787
- dev_tools_prune_errors = None ,
1788
- ):
1780
+ debug : Optional [ bool ] = None ,
1781
+ dev_tools_ui : Optional [ bool ] = None ,
1782
+ dev_tools_props_check : Optional [ bool ] = None ,
1783
+ dev_tools_serve_dev_bundles : Optional [ bool ] = None ,
1784
+ dev_tools_hot_reload : Optional [ bool ] = None ,
1785
+ dev_tools_hot_reload_interval : Optional [ int ] = None ,
1786
+ dev_tools_hot_reload_watch_interval : Optional [ int ] = None ,
1787
+ dev_tools_hot_reload_max_retry : Optional [ int ] = None ,
1788
+ dev_tools_silence_routes_logging : Optional [ bool ] = None ,
1789
+ dev_tools_disable_version_check : Optional [ bool ] = None ,
1790
+ dev_tools_prune_errors : Optional [ bool ] = None ,
1791
+ ) -> None :
1789
1792
"""Activate the dev tools, called by `run`. If your application
1790
1793
is served by wsgi and you want to activate the dev tools, you can call
1791
1794
this method out of `__main__`.
@@ -2275,7 +2278,7 @@ def verify_url_part(served_part, url_part, part_name):
2275
2278
else :
2276
2279
self .server .run (host = host , port = port , debug = debug , ** flask_run_options )
2277
2280
2278
- def enable_pages (self ):
2281
+ def enable_pages (self ) -> None :
2279
2282
if not self .use_pages :
2280
2283
return
2281
2284
if self .pages_folder :
0 commit comments