|
1 |
| -from dash import Dash, dcc, html |
| 1 | +from dash import Dash, dcc, html, Input, Output |
2 | 2 |
|
3 | 3 |
|
4 | 4 | def test_mkdw001_img(dash_dcc):
|
@@ -147,3 +147,91 @@ def test_mkdw005_block_mathjax(dash_dcc):
|
147 | 147 | dash_dcc.start_server(app)
|
148 | 148 | dash_dcc.percy_snapshot("mkdw005 - markdown block mathjax")
|
149 | 149 | assert dash_dcc.get_logs() == []
|
| 150 | + |
| 151 | + |
| 152 | +def test_mkdw006_toggle_mathjax(dash_dcc): |
| 153 | + app = Dash(__name__) |
| 154 | + |
| 155 | + gravity = "$F=\\frac{Gm_1m_2}{r^2}$" |
| 156 | + |
| 157 | + app.layout = html.Div( |
| 158 | + [ |
| 159 | + html.Button("Toggle MathJax", id="btn"), |
| 160 | + dcc.Markdown( |
| 161 | + f""" |
| 162 | + # Test MathJax Toggling {gravity} |
| 163 | + """, |
| 164 | + id="md", |
| 165 | + ), |
| 166 | + ] |
| 167 | + ) |
| 168 | + |
| 169 | + @app.callback( |
| 170 | + Output("md", "mathjax"), Input("btn", "n_clicks"), prevent_initial_call=True |
| 171 | + ) |
| 172 | + def toggle(n): |
| 173 | + return (n or 0) % 2 != 0 |
| 174 | + |
| 175 | + dash_dcc.start_server(app) |
| 176 | + |
| 177 | + # Initial state: no MathJax loaded or rendered, unformatted text is shown |
| 178 | + dash_dcc.wait_for_contains_text("#md", gravity) |
| 179 | + dash_dcc.wait_for_no_elements("#md svg") |
| 180 | + assert not dash_dcc.driver.execute_script("return !!window.MathJax") |
| 181 | + |
| 182 | + btn = dash_dcc.find_element("#btn") |
| 183 | + btn.click() |
| 184 | + |
| 185 | + # One click: MathJax is rendered, unformatted text is gone |
| 186 | + |
| 187 | + dash_dcc.wait_for_element("#md svg") |
| 188 | + assert gravity not in dash_dcc._get_element("#md").text |
| 189 | + assert dash_dcc.driver.execute_script("return !!window.MathJax") |
| 190 | + |
| 191 | + btn.click() |
| 192 | + |
| 193 | + # Second click: Back to initial state except that MathJax library is still loaded |
| 194 | + dash_dcc.wait_for_contains_text("#md", gravity) |
| 195 | + dash_dcc.wait_for_no_elements("#md svg") |
| 196 | + assert dash_dcc.driver.execute_script("return !!window.MathJax") |
| 197 | + |
| 198 | + |
| 199 | +def test_mkdw007_load_mathjax(dash_dcc): |
| 200 | + app = Dash(__name__) |
| 201 | + |
| 202 | + gravity = "$F=\\frac{Gm_1m_2}{r^2}$" |
| 203 | + |
| 204 | + app.layout = html.Div( |
| 205 | + [ |
| 206 | + html.Button("Add Second MathJax", id="btn"), |
| 207 | + dcc.Markdown( |
| 208 | + f""" |
| 209 | + # No Math Rendering Here! {gravity} |
| 210 | + """, |
| 211 | + id="md", |
| 212 | + ), |
| 213 | + html.Div("initial", id="out"), |
| 214 | + ] |
| 215 | + ) |
| 216 | + |
| 217 | + @app.callback( |
| 218 | + Output("out", "children"), Input("btn", "n_clicks"), prevent_initial_call=True |
| 219 | + ) |
| 220 | + def add_math(n): |
| 221 | + return dcc.Markdown(f"# Math!\n{gravity}", id="md2", mathjax=True) |
| 222 | + |
| 223 | + dash_dcc.start_server(app) |
| 224 | + |
| 225 | + # Initial state: no MathJax loaded or rendered, unformatted text is shown |
| 226 | + dash_dcc.wait_for_contains_text("#md", gravity) |
| 227 | + dash_dcc.wait_for_no_elements("#md svg") |
| 228 | + assert not dash_dcc.driver.execute_script("return !!window.MathJax") |
| 229 | + |
| 230 | + btn = dash_dcc.find_element("#btn") |
| 231 | + btn.click() |
| 232 | + |
| 233 | + # One click: MathJax is loaded and rendered on the second, unformatted text is gone |
| 234 | + |
| 235 | + dash_dcc.wait_for_element("#md2 svg") |
| 236 | + assert gravity not in dash_dcc._get_element("#md2").text |
| 237 | + assert dash_dcc.driver.execute_script("return !!window.MathJax") |
0 commit comments