7
7
import unittest .mock as mock
8
8
from plotly .express ._core import build_dataframe
9
9
from pandas .testing import assert_frame_equal
10
+ from plotly .tests .b64 import b64 , _b64
10
11
import sys
11
12
import warnings
12
13
@@ -28,8 +29,9 @@ def add_interchange_module_for_old_pandas():
28
29
29
30
def test_numpy ():
30
31
fig = px .scatter (x = [1 , 2 , 3 ], y = [2 , 3 , 4 ], color = [1 , 3 , 9 ])
31
- assert np .all (fig .data [0 ].x == np .array ([1 , 2 , 3 ]))
32
- assert np .all (fig .data [0 ].y == np .array ([2 , 3 , 4 ]))
32
+
33
+ assert np .all (fig .data [0 ].x == b64 (np .array ([1 , 2 , 3 ])))
34
+ assert np .all (fig .data [0 ].y == b64 (np .array ([2 , 3 , 4 ])))
33
35
assert np .all (fig .data [0 ].marker .color == np .array ([1 , 3 , 9 ]))
34
36
35
37
@@ -101,16 +103,16 @@ def test_several_dataframes():
101
103
df = pd .DataFrame (dict (x = [0 , 1 ], y = [3 , 4 ]))
102
104
df2 = pd .DataFrame (dict (x = [3 , 5 ], y = [23 , 24 ]))
103
105
fig = px .scatter (x = df .y , y = df2 .y )
104
- assert np .all (fig .data [0 ].x == np .array ([3 , 4 ]))
105
- assert np .all (fig .data [0 ].y == np .array ([23 , 24 ]))
106
+ assert np .all (fig .data [0 ].x == b64 ( np .array ([3 , 4 ]) ))
107
+ assert np .all (fig .data [0 ].y == b64 ( np .array ([23 , 24 ]) ))
106
108
assert fig .data [0 ].hovertemplate == "x=%{x}<br>y=%{y}<extra></extra>"
107
109
108
110
df = pd .DataFrame (dict (x = [0 , 1 ], y = [3 , 4 ]))
109
111
df2 = pd .DataFrame (dict (x = [3 , 5 ], y = [23 , 24 ]))
110
112
df3 = pd .DataFrame (dict (y = [0.1 , 0.2 ]))
111
113
fig = px .scatter (x = df .y , y = df2 .y , size = df3 .y )
112
- assert np .all (fig .data [0 ].x == np .array ([3 , 4 ]))
113
- assert np .all (fig .data [0 ].y == np .array ([23 , 24 ]))
114
+ assert np .all (fig .data [0 ].x == b64 ( np .array ([3 , 4 ]) ))
115
+ assert np .all (fig .data [0 ].y == b64 ( np .array ([23 , 24 ]) ))
114
116
assert (
115
117
fig .data [0 ].hovertemplate
116
118
== "x=%{x}<br>y=%{y}<br>size=%{marker.size}<extra></extra>"
@@ -120,8 +122,8 @@ def test_several_dataframes():
120
122
df2 = pd .DataFrame (dict (x = [3 , 5 ], y = [23 , 24 ]))
121
123
df3 = pd .DataFrame (dict (y = [0.1 , 0.2 ]))
122
124
fig = px .scatter (x = df .y , y = df2 .y , hover_data = [df3 .y ])
123
- assert np .all (fig .data [0 ].x == np .array ([3 , 4 ]))
124
- assert np .all (fig .data [0 ].y == np .array ([23 , 24 ]))
125
+ assert np .all (fig .data [0 ].x == b64 ( np .array ([3 , 4 ]) ))
126
+ assert np .all (fig .data [0 ].y == b64 ( np .array ([23 , 24 ]) ))
125
127
assert (
126
128
fig .data [0 ].hovertemplate
127
129
== "x=%{x}<br>y=%{y}<br>hover_data_0=%{customdata[0]}<extra></extra>"
@@ -131,8 +133,8 @@ def test_several_dataframes():
131
133
def test_name_heuristics ():
132
134
df = pd .DataFrame (dict (x = [0 , 1 ], y = [3 , 4 ], z = [0.1 , 0.2 ]))
133
135
fig = px .scatter (df , x = df .y , y = df .x , size = df .y )
134
- assert np .all (fig .data [0 ].x == np .array ([3 , 4 ]))
135
- assert np .all (fig .data [0 ].y == np .array ([0 , 1 ]))
136
+ assert np .all (fig .data [0 ].x == b64 ( np .array ([3 , 4 ]) ))
137
+ assert np .all (fig .data [0 ].y == b64 ( np .array ([0 , 1 ]) ))
136
138
assert fig .data [0 ].hovertemplate == "y=%{marker.size}<br>x=%{y}<extra></extra>"
137
139
138
140
@@ -405,27 +407,27 @@ def test_splom_case():
405
407
assert len (fig .data [0 ].dimensions ) == len (iris .columns )
406
408
dic = {"a" : [1 , 2 , 3 ], "b" : [4 , 5 , 6 ], "c" : [7 , 8 , 9 ]}
407
409
fig = px .scatter_matrix (dic )
408
- assert np .all (fig .data [0 ].dimensions [0 ].values == np .array (dic ["a" ]))
410
+ assert np .all (fig .data [0 ].dimensions [0 ].values == b64 ( np .array (dic ["a" ]) ))
409
411
ar = np .arange (9 ).reshape ((3 , 3 ))
410
412
fig = px .scatter_matrix (ar )
411
- assert np .all (fig .data [0 ].dimensions [0 ].values == ar [:, 0 ])
413
+ assert np .all (fig .data [0 ].dimensions [0 ].values == b64 ( ar [:, 0 ]) )
412
414
413
415
414
416
def test_int_col_names ():
415
417
# DataFrame with int column names
416
418
lengths = pd .DataFrame (np .random .random (100 ))
417
419
fig = px .histogram (lengths , x = 0 )
418
- assert np .all (np .array (lengths ).flatten () == fig .data [0 ].x )
420
+ assert np .all (b64 ( np .array (lengths ).flatten () ) == fig .data [0 ].x )
419
421
# Numpy array
420
422
ar = np .arange (100 ).reshape ((10 , 10 ))
421
423
fig = px .scatter (ar , x = 2 , y = 8 )
422
- assert np .all (fig .data [0 ].x == ar [:, 2 ])
424
+ assert np .all (fig .data [0 ].x == b64 ( ar [:, 2 ]) )
423
425
424
426
425
427
def test_data_frame_from_dict ():
426
428
fig = px .scatter ({"time" : [0 , 1 ], "money" : [1 , 2 ]}, x = "time" , y = "money" )
427
429
assert fig .data [0 ].hovertemplate == "time=%{x}<br>money=%{y}<extra></extra>"
428
- assert np .all (fig .data [0 ].x == [0 , 1 ])
430
+ assert np .all (fig .data [0 ].x == _b64 ( [0 , 1 ]) )
429
431
430
432
431
433
def test_arguments_not_modified ():
@@ -489,13 +491,11 @@ def test_identity_map():
489
491
490
492
def test_constants ():
491
493
fig = px .scatter (x = px .Constant (1 ), y = [1 , 2 ])
492
- assert fig .data [0 ].x [0 ] == 1
493
- assert fig .data [0 ].x [1 ] == 1
494
+ assert fig .data [0 ].x == _b64 ([1 , 1 ])
494
495
assert "x=" in fig .data [0 ].hovertemplate
495
496
496
497
fig = px .scatter (x = px .Constant (1 , label = "time" ), y = [1 , 2 ])
497
- assert fig .data [0 ].x [0 ] == 1
498
- assert fig .data [0 ].x [1 ] == 1
498
+ assert fig .data [0 ].x == _b64 ([1 , 1 ])
499
499
assert "x=" not in fig .data [0 ].hovertemplate
500
500
assert "time=" in fig .data [0 ].hovertemplate
501
501
@@ -519,15 +519,12 @@ def test_constants():
519
519
520
520
def test_ranges ():
521
521
fig = px .scatter (x = px .Range (), y = [1 , 2 ], hover_data = [px .Range ()])
522
- assert fig .data [0 ].x [0 ] == 0
523
- assert fig .data [0 ].x [1 ] == 1
524
- assert fig .data [0 ].customdata [0 ][0 ] == 0
525
- assert fig .data [0 ].customdata [1 ][0 ] == 1
522
+ assert fig .data [0 ].x == _b64 ([0 , 1 ])
523
+ assert fig .data [0 ].customdata == _b64 ([[0 ], [1 ]])
526
524
assert "x=" in fig .data [0 ].hovertemplate
527
525
528
526
fig = px .scatter (x = px .Range (label = "time" ), y = [1 , 2 ])
529
- assert fig .data [0 ].x [0 ] == 0
530
- assert fig .data [0 ].x [1 ] == 1
527
+ assert fig .data [0 ].x == _b64 ([0 , 1 ])
531
528
assert "x=" not in fig .data [0 ].hovertemplate
532
529
assert "time=" in fig .data [0 ].hovertemplate
533
530
@@ -617,55 +614,55 @@ def test_x_or_y(fn):
617
614
categorical_df = pd .DataFrame (dict (col = categorical ), index = index )
618
615
619
616
fig = fn (x = numerical )
620
- assert list ( fig .data [0 ].x ) == numerical
621
- assert list ( fig .data [0 ].y ) == range_4
617
+ assert fig .data [0 ].x == _b64 ( numerical )
618
+ assert fig .data [0 ].y == _b64 ( range_4 )
622
619
assert fig .data [0 ].orientation == "h"
623
620
fig = fn (y = numerical )
624
- assert list ( fig .data [0 ].x ) == range_4
625
- assert list ( fig .data [0 ].y ) == numerical
621
+ assert fig .data [0 ].x == _b64 ( range_4 )
622
+ assert fig .data [0 ].y == _b64 ( numerical )
626
623
assert fig .data [0 ].orientation == "v"
627
624
fig = fn (numerical_df , x = "col" )
628
- assert list ( fig .data [0 ].x ) == numerical
629
- assert list ( fig .data [0 ].y ) == index
625
+ assert fig .data [0 ].x == _b64 ( numerical )
626
+ assert fig .data [0 ].y == _b64 ( index )
630
627
assert fig .data [0 ].orientation == "h"
631
628
fig = fn (numerical_df , y = "col" )
632
- assert list ( fig .data [0 ].x ) == index
633
- assert list ( fig .data [0 ].y ) == numerical
629
+ assert fig .data [0 ].x == _b64 ( index )
630
+ assert fig .data [0 ].y == _b64 ( numerical )
634
631
assert fig .data [0 ].orientation == "v"
635
632
636
633
if fn != px .bar :
637
634
fig = fn (x = categorical )
638
635
assert list (fig .data [0 ].x ) == categorical
639
- assert list ( fig .data [0 ].y ) == range_4
636
+ assert fig .data [0 ].y == _b64 ( range_4 )
640
637
assert fig .data [0 ].orientation == "h"
641
638
fig = fn (y = categorical )
642
- assert list ( fig .data [0 ].x ) == range_4
639
+ assert fig .data [0 ].x == _b64 ( range_4 )
643
640
assert list (fig .data [0 ].y ) == categorical
644
641
assert fig .data [0 ].orientation == "v"
645
642
fig = fn (categorical_df , x = "col" )
646
643
assert list (fig .data [0 ].x ) == categorical
647
- assert list ( fig .data [0 ].y ) == index
644
+ assert fig .data [0 ].y == _b64 ( index )
648
645
assert fig .data [0 ].orientation == "h"
649
646
fig = fn (categorical_df , y = "col" )
650
- assert list ( fig .data [0 ].x ) == index
647
+ assert fig .data [0 ].x == _b64 ( index )
651
648
assert list (fig .data [0 ].y ) == categorical
652
649
assert fig .data [0 ].orientation == "v"
653
650
654
651
else :
655
652
fig = fn (x = categorical )
656
653
assert list (fig .data [0 ].x ) == categorical
657
- assert list ( fig .data [0 ].y ) == constant
654
+ assert fig .data [0 ].y == _b64 ( constant )
658
655
assert fig .data [0 ].orientation == "v"
659
656
fig = fn (y = categorical )
660
- assert list ( fig .data [0 ].x ) == constant
657
+ assert fig .data [0 ].x == _b64 ( constant )
661
658
assert list (fig .data [0 ].y ) == categorical
662
659
assert fig .data [0 ].orientation == "h"
663
660
fig = fn (categorical_df , x = "col" )
664
661
assert list (fig .data [0 ].x ) == categorical
665
- assert list ( fig .data [0 ].y ) == constant
662
+ assert fig .data [0 ].y == _b64 ( constant )
666
663
assert fig .data [0 ].orientation == "v"
667
664
fig = fn (categorical_df , y = "col" )
668
- assert list ( fig .data [0 ].x ) == constant
665
+ assert fig .data [0 ].x == _b64 ( constant )
669
666
assert list (fig .data [0 ].y ) == categorical
670
667
assert fig .data [0 ].orientation == "h"
671
668
0 commit comments