99from selenium .webdriver .support import expected_conditions as EC
1010
1111
12- def findSyncPlotlyJs (scripts ):
13- for script in scripts :
14- if "dash_core_components/plotly-" in script .get_attribute ("src" ):
15- return script
16-
17-
18- def findAsyncPlotlyJs (scripts ):
19- for script in scripts :
20- if "dash_core_components/async-plotlyjs" in script .get_attribute ("src" ):
21- return script
22-
23-
24- def findAsyncMathJax (scripts ):
25- for script in scripts :
26- if "dash_core_components/async-mathjax" in script .get_attribute ("src" ):
27- return script
28-
29-
30- def findSyncMathJax (scripts ):
31- for script in scripts :
32- if "dash_core_components/mathjax" in script .get_attribute ("src" ):
33- return script
34-
35-
3612@pytest .mark .parametrize ("is_eager" , [True , False ])
3713def test_grva001_candlestick (dash_dcc , is_eager ):
3814 app = Dash (__name__ , eager_loading = is_eager )
@@ -99,7 +75,7 @@ def test_grva002_graphs_with_different_figures(dash_dcc, is_eager):
9975 "x" : [1 , 2 , 3 ],
10076 "y" : [2 , 4 , 5 ],
10177 "type" : "bar" ,
102- "name" : u "Montréal" ,
78+ "name" : "Montréal" ,
10379 },
10480 ],
10581 "layout" : {"title" : "Dash Data Visualization" },
@@ -119,7 +95,7 @@ def test_grva002_graphs_with_different_figures(dash_dcc, is_eager):
11995 "x" : [11 , 22 , 33 ],
12096 "y" : [22 , 44 , 55 ],
12197 "type" : "bar" ,
122- "name" : u "Montréal" ,
98+ "name" : "Montréal" ,
12399 },
124100 ],
125101 "layout" : {"title" : "Dash Data Visualization" },
@@ -579,7 +555,7 @@ def test_grva006_unmounted_graph_resize(dash_dcc, is_eager):
579555 "x" : [1 , 2 , 3 ],
580556 "y" : [2 , 4 , 5 ],
581557 "type" : "scattergl" ,
582- "name" : u "Montréal" ,
558+ "name" : "Montréal" ,
583559 },
584560 ]
585561 },
@@ -606,7 +582,7 @@ def test_grva006_unmounted_graph_resize(dash_dcc, is_eager):
606582 "x" : [1 , 2 , 3 ],
607583 "y" : [1 , 2 , 3 ],
608584 "type" : "scattergl" ,
609- "name" : u "Montréal" ,
585+ "name" : "Montréal" ,
610586 },
611587 ]
612588 },
@@ -649,11 +625,14 @@ def test_grva006_unmounted_graph_resize(dash_dcc, is_eager):
649625 assert dash_dcc .get_logs () == []
650626
651627
652- def test_grva007_external_plotlyjs_prevents_lazy (dash_dcc ):
628+ @pytest .mark .parametrize ("is_eager" , [False , True ])
629+ def test_grva007_external_plotlyjs_prevents_lazy (is_eager , dash_dcc ):
630+ # specific plotly.js version that's older than the built-in version
631+ v = "2.8.1"
653632 app = Dash (
654633 __name__ ,
655- eager_loading = False ,
656- external_scripts = ["https://unpkg.com/plotly.js-dist-min/plotly.min.js" ],
634+ eager_loading = is_eager ,
635+ external_scripts = [f "https://unpkg.com/plotly.js-dist-min@ { v } /plotly.min.js" ],
657636 )
658637
659638 app .layout = html .Div (id = "div" , children = [html .Button (id = "btn" )])
@@ -676,18 +655,22 @@ def load_chart(n_clicks):
676655 # Give time for the async dependency to be requested (if any)
677656 time .sleep (2 )
678657
679- scripts = dash_dcc .driver .find_elements (By .CSS_SELECTOR , "script" )
680- assert findSyncPlotlyJs (scripts ) is None
681- assert findAsyncPlotlyJs (scripts ) is None
658+ v_loaded = dash_dcc .driver .execute_script ("return Plotly.version" )
659+
660+ # TODO: in eager mode, built-in plotly.js wins!! I don't think this is what we want.
661+ # But need to look into why we use the bare plotly.js bundle in eager mode, rather
662+ # than simply preloading the regular async chunk.
663+ if not is_eager :
664+ # as loaded in external_scripts
665+ assert v_loaded == v
682666
683667 dash_dcc .find_element ("#btn" ).click ()
684668
685669 # Give time for the async dependency to be requested (if any)
686670 time .sleep (2 )
687671
688- scripts = dash_dcc .driver .find_elements (By .CSS_SELECTOR , "script" )
689- assert findSyncPlotlyJs (scripts ) is None
690- assert findAsyncPlotlyJs (scripts ) is None
672+ # Check that the originally-loaded version is still the one we have
673+ assert dash_dcc .driver .execute_script ("return Plotly.version" ) == v_loaded
691674
692675 assert dash_dcc .get_logs () == []
693676
@@ -862,10 +845,12 @@ def graph_dims():
862845
863846
864847def test_grva010_external_mathjax_prevents_lazy (dash_dcc ):
848+ # specific MathJax version that's older than the built-in version
849+ v = "3.1.4"
865850 app = Dash (
866851 __name__ ,
867852 eager_loading = False ,
868- external_scripts = ["https://cdn.jsdelivr.net/npm/mathjax@3.2.0 /es5/tex-svg.js" ],
853+ external_scripts = [f "https://cdn.jsdelivr.net/npm/mathjax@{ v } /es5/tex-svg.js" ],
869854 )
870855
871856 app .layout = html .Div (id = "div" , children = [html .Button (id = "btn" )])
@@ -895,16 +880,16 @@ def load_chart(n_clicks):
895880 # Give time for the async dependency to be requested (if any)
896881 dash_dcc .wait_for_element ("button#btn" )
897882
898- scripts = dash_dcc .driver .find_elements (By .CSS_SELECTOR , "script" )
899- assert findSyncMathJax (scripts ) is None
900- assert findAsyncMathJax (scripts ) is None
883+ # even in eager mode (when the async bundle is preloaded) we keep the
884+ # external version, which seems to be a pleasant effect of how
885+ # webpack bundles these chunks!
886+ assert dash_dcc .driver .execute_script ("return MathJax.version" ) == v
901887
902888 dash_dcc .find_element ("#btn" ).click ()
903889 dash_dcc .wait_for_element (".gtitle-math" )
904890
905- scripts = dash_dcc .driver .find_elements (By .CSS_SELECTOR , "script" )
906- assert findSyncMathJax (scripts ) is None
907- assert findAsyncMathJax (scripts ) is None
891+ # We still have the external version, not the built-in one
892+ assert dash_dcc .driver .execute_script ("return MathJax.version" ) == v
908893
909894 assert dash_dcc .get_logs () == []
910895
@@ -928,9 +913,7 @@ def test_grva011_without_mathjax(dash_dcc, is_eager):
928913 dash_dcc .start_server (app )
929914 assert dash_dcc .wait_for_element (".gtitle" ).text == "Apple: $2, Orange: $3"
930915
931- scripts = dash_dcc .driver .find_elements (By .CSS_SELECTOR , "script" )
932- assert findSyncMathJax (scripts ) is None
933- assert findAsyncMathJax (scripts ) is None
916+ assert not dash_dcc .driver .execute_script ("return !!Window.MathJax" )
934917
935918 assert dash_dcc .get_logs () == []
936919
@@ -955,9 +938,7 @@ def test_grva012_with_mathjax(dash_dcc, is_eager):
955938 dash_dcc .start_server (app )
956939 dash_dcc .wait_for_element (".gtitle-math" )
957940
958- scripts = dash_dcc .driver .find_elements (By .CSS_SELECTOR , "script" )
959- assert findSyncMathJax (scripts ) is None
960- assert findAsyncMathJax (scripts ) is None
941+ assert dash_dcc .driver .execute_script ("return !!Window.MathJax" )
961942
962943 assert dash_dcc .get_logs () == []
963944
@@ -991,6 +972,9 @@ def toggle(n):
991972
992973 # Initial state: no MathJax loaded or rendered, unformatted text is shown
993974 dash_dcc .wait_for_contains_text (".gtitle" , gravity )
975+
976+ # Note: in eager mode, the async-mathjax bundle IS loaded, but it seems like
977+ # it isn't executed until we ask for MathJax with import()
994978 assert not dash_dcc .driver .execute_script ("return !!window.MathJax" )
995979
996980 btn = dash_dcc .find_element ("#btn" )
0 commit comments