| 113 | |
| 114 | |
| 115 | def test_px_templates(backend): |
| 116 | try: |
| 117 | import plotly.graph_objects as go |
| 118 | |
| 119 | tips = px.data.tips(return_type=backend) |
| 120 | |
| 121 | # use the normal defaults |
| 122 | fig = px.scatter() |
| 123 | assert fig.layout.template == pio.templates[pio.templates.default] |
| 124 | |
| 125 | # respect changes to defaults |
| 126 | pio.templates.default = "seaborn" |
| 127 | fig = px.scatter() |
| 128 | assert fig.layout.template == pio.templates["seaborn"] |
| 129 | |
| 130 | # special px-level defaults over pio defaults |
| 131 | pio.templates.default = "seaborn" |
| 132 | px.defaults.template = "ggplot2" |
| 133 | fig = px.scatter() |
| 134 | assert fig.layout.template == pio.templates["ggplot2"] |
| 135 | |
| 136 | # accept names in args over pio and px defaults |
| 137 | fig = px.scatter(template="seaborn") |
| 138 | assert fig.layout.template == pio.templates["seaborn"] |
| 139 | |
| 140 | # accept objects in args |
| 141 | fig = px.scatter(template={}) |
| 142 | assert fig.layout.template == go.layout.Template(data_scatter=[{}]) |
| 143 | |
| 144 | # read colorway from the template |
| 145 | fig = px.scatter( |
| 146 | tips, |
| 147 | x="total_bill", |
| 148 | y="tip", |
| 149 | color="sex", |
| 150 | template=dict(layout_colorway=["red", "blue"]), |
| 151 | ) |
| 152 | assert fig.data[0].marker.color == "red" |
| 153 | assert fig.data[1].marker.color == "blue" |
| 154 | |
| 155 | # default colorway fallback |
| 156 | fig = px.scatter(tips, x="total_bill", y="tip", color="sex", template=dict()) |
| 157 | assert fig.data[0].marker.color == px.colors.qualitative.D3[0] |
| 158 | assert fig.data[1].marker.color == px.colors.qualitative.D3[1] |
| 159 | |
| 160 | # pio default template colorway fallback |
| 161 | pio.templates.default = "seaborn" |
| 162 | px.defaults.template = None |
| 163 | fig = px.scatter(tips, x="total_bill", y="tip", color="sex") |
| 164 | assert fig.data[0].marker.color == pio.templates["seaborn"].layout.colorway[0] |
| 165 | assert fig.data[1].marker.color == pio.templates["seaborn"].layout.colorway[1] |
| 166 | |
| 167 | # pio default template colorway fallback |
| 168 | pio.templates.default = "seaborn" |
| 169 | px.defaults.template = "ggplot2" |
| 170 | fig = px.scatter(tips, x="total_bill", y="tip", color="sex") |
| 171 | assert fig.data[0].marker.color == pio.templates["ggplot2"].layout.colorway[0] |
| 172 | assert fig.data[1].marker.color == pio.templates["ggplot2"].layout.colorway[1] |