9
9
from selenium .webdriver .support import expected_conditions as EC
10
10
11
11
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
-
36
12
@pytest .mark .parametrize ("is_eager" , [True , False ])
37
13
def test_grva001_candlestick (dash_dcc , is_eager ):
38
14
app = Dash (__name__ , eager_loading = is_eager )
@@ -99,7 +75,7 @@ def test_grva002_graphs_with_different_figures(dash_dcc, is_eager):
99
75
"x" : [1 , 2 , 3 ],
100
76
"y" : [2 , 4 , 5 ],
101
77
"type" : "bar" ,
102
- "name" : u "Montréal" ,
78
+ "name" : "Montréal" ,
103
79
},
104
80
],
105
81
"layout" : {"title" : "Dash Data Visualization" },
@@ -119,7 +95,7 @@ def test_grva002_graphs_with_different_figures(dash_dcc, is_eager):
119
95
"x" : [11 , 22 , 33 ],
120
96
"y" : [22 , 44 , 55 ],
121
97
"type" : "bar" ,
122
- "name" : u "Montréal" ,
98
+ "name" : "Montréal" ,
123
99
},
124
100
],
125
101
"layout" : {"title" : "Dash Data Visualization" },
@@ -579,7 +555,7 @@ def test_grva006_unmounted_graph_resize(dash_dcc, is_eager):
579
555
"x" : [1 , 2 , 3 ],
580
556
"y" : [2 , 4 , 5 ],
581
557
"type" : "scattergl" ,
582
- "name" : u "Montréal" ,
558
+ "name" : "Montréal" ,
583
559
},
584
560
]
585
561
},
@@ -606,7 +582,7 @@ def test_grva006_unmounted_graph_resize(dash_dcc, is_eager):
606
582
"x" : [1 , 2 , 3 ],
607
583
"y" : [1 , 2 , 3 ],
608
584
"type" : "scattergl" ,
609
- "name" : u "Montréal" ,
585
+ "name" : "Montréal" ,
610
586
},
611
587
]
612
588
},
@@ -649,11 +625,14 @@ def test_grva006_unmounted_graph_resize(dash_dcc, is_eager):
649
625
assert dash_dcc .get_logs () == []
650
626
651
627
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"
653
632
app = Dash (
654
633
__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" ],
657
636
)
658
637
659
638
app .layout = html .Div (id = "div" , children = [html .Button (id = "btn" )])
@@ -676,18 +655,22 @@ def load_chart(n_clicks):
676
655
# Give time for the async dependency to be requested (if any)
677
656
time .sleep (2 )
678
657
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
682
666
683
667
dash_dcc .find_element ("#btn" ).click ()
684
668
685
669
# Give time for the async dependency to be requested (if any)
686
670
time .sleep (2 )
687
671
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
691
674
692
675
assert dash_dcc .get_logs () == []
693
676
@@ -862,10 +845,12 @@ def graph_dims():
862
845
863
846
864
847
def 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"
865
850
app = Dash (
866
851
__name__ ,
867
852
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" ],
869
854
)
870
855
871
856
app .layout = html .Div (id = "div" , children = [html .Button (id = "btn" )])
@@ -895,16 +880,16 @@ def load_chart(n_clicks):
895
880
# Give time for the async dependency to be requested (if any)
896
881
dash_dcc .wait_for_element ("button#btn" )
897
882
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
901
887
902
888
dash_dcc .find_element ("#btn" ).click ()
903
889
dash_dcc .wait_for_element (".gtitle-math" )
904
890
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
908
893
909
894
assert dash_dcc .get_logs () == []
910
895
@@ -928,9 +913,7 @@ def test_grva011_without_mathjax(dash_dcc, is_eager):
928
913
dash_dcc .start_server (app )
929
914
assert dash_dcc .wait_for_element (".gtitle" ).text == "Apple: $2, Orange: $3"
930
915
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" )
934
917
935
918
assert dash_dcc .get_logs () == []
936
919
@@ -955,9 +938,7 @@ def test_grva012_with_mathjax(dash_dcc, is_eager):
955
938
dash_dcc .start_server (app )
956
939
dash_dcc .wait_for_element (".gtitle-math" )
957
940
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" )
961
942
962
943
assert dash_dcc .get_logs () == []
963
944
@@ -991,6 +972,9 @@ def toggle(n):
991
972
992
973
# Initial state: no MathJax loaded or rendered, unformatted text is shown
993
974
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()
994
978
assert not dash_dcc .driver .execute_script ("return !!window.MathJax" )
995
979
996
980
btn = dash_dcc .find_element ("#btn" )
0 commit comments