|
1 | 1 | from multiprocessing import Lock
|
2 | 2 | from dash import Dash, Input, Output, dcc, html
|
3 | 3 | from dash.testing import wait
|
| 4 | +import time |
4 | 5 |
|
5 | 6 |
|
6 | 7 | def test_ldcp001_loading_component_initialization(dash_dcc):
|
@@ -366,3 +367,324 @@ def test_ldcp008_graph_in_loading_fits_container_height(dash_dcc):
|
366 | 367 | ) == dash_dcc.wait_for_element(".outer-container").size.get("height")
|
367 | 368 |
|
368 | 369 | assert dash_dcc.get_logs() == []
|
| 370 | + |
| 371 | + |
| 372 | +def test_ldcp009_loading_component_overlay_style(dash_dcc): |
| 373 | + lock = Lock() |
| 374 | + |
| 375 | + app = Dash(__name__) |
| 376 | + |
| 377 | + app.layout = html.Div( |
| 378 | + [ |
| 379 | + dcc.Loading( |
| 380 | + [html.Div(id="div-1")], |
| 381 | + className="loading", |
| 382 | + overlay_style={ |
| 383 | + "visibility": "visible", |
| 384 | + "opacity": 0.5, |
| 385 | + "backgroundColor": "white", |
| 386 | + }, |
| 387 | + ) |
| 388 | + ], |
| 389 | + id="root", |
| 390 | + ) |
| 391 | + |
| 392 | + @app.callback(Output("div-1", "children"), [Input("root", "n_clicks")]) |
| 393 | + def updateDiv(n_clicks): |
| 394 | + if n_clicks is not None: |
| 395 | + with lock: |
| 396 | + return "changed" |
| 397 | + |
| 398 | + return "content" |
| 399 | + |
| 400 | + with lock: |
| 401 | + dash_dcc.start_server(app) |
| 402 | + dash_dcc.wait_for_text_to_equal("#div-1", "content") |
| 403 | + |
| 404 | + dash_dcc.find_element("#root").click() |
| 405 | + |
| 406 | + dash_dcc.find_element(".loading .dash-spinner") |
| 407 | + # unlike the default, the content should be visible |
| 408 | + dash_dcc.wait_for_text_to_equal("#div-1", "content") |
| 409 | + dash_dcc.wait_for_style_to_equal("#root > div", "opacity", "0.5") |
| 410 | + |
| 411 | + dash_dcc.wait_for_text_to_equal("#div-1", "changed") |
| 412 | + |
| 413 | + assert dash_dcc.get_logs() == [] |
| 414 | + |
| 415 | + |
| 416 | +# multiple components, only one triggers the spinner |
| 417 | +def test_ldcp010_loading_component_target_components(dash_dcc): |
| 418 | + |
| 419 | + lock = Lock() |
| 420 | + |
| 421 | + app = Dash(__name__) |
| 422 | + |
| 423 | + app.layout = html.Div( |
| 424 | + [ |
| 425 | + dcc.Loading( |
| 426 | + [ |
| 427 | + html.Button(id="btn-1"), |
| 428 | + html.Button(id="btn-2"), |
| 429 | + ], |
| 430 | + className="loading-1", |
| 431 | + target_components={"btn-2": "children"}, |
| 432 | + ) |
| 433 | + ], |
| 434 | + id="root", |
| 435 | + ) |
| 436 | + |
| 437 | + @app.callback(Output("btn-1", "children"), [Input("btn-2", "n_clicks")]) |
| 438 | + def updateDiv1(n_clicks): |
| 439 | + if n_clicks: |
| 440 | + with lock: |
| 441 | + return "changed 1" |
| 442 | + |
| 443 | + return "content 1" |
| 444 | + |
| 445 | + @app.callback(Output("btn-2", "children"), [Input("btn-1", "n_clicks")]) |
| 446 | + def updateDiv2(n_clicks): |
| 447 | + if n_clicks: |
| 448 | + with lock: |
| 449 | + return "changed 2" |
| 450 | + |
| 451 | + return "content 2" |
| 452 | + |
| 453 | + dash_dcc.start_server(app) |
| 454 | + |
| 455 | + dash_dcc.wait_for_text_to_equal("#btn-1", "content 1") |
| 456 | + dash_dcc.wait_for_text_to_equal("#btn-2", "content 2") |
| 457 | + |
| 458 | + with lock: |
| 459 | + dash_dcc.find_element("#btn-1").click() |
| 460 | + |
| 461 | + dash_dcc.find_element(".loading-1 .dash-spinner") |
| 462 | + dash_dcc.wait_for_text_to_equal("#btn-2", "") |
| 463 | + |
| 464 | + dash_dcc.wait_for_text_to_equal("#btn-2", "changed 2") |
| 465 | + |
| 466 | + with lock: |
| 467 | + dash_dcc.find_element("#btn-2").click() |
| 468 | + spinners = dash_dcc.find_elements(".loading-1 .dash-spinner") |
| 469 | + dash_dcc.wait_for_text_to_equal("#btn-1", "") |
| 470 | + |
| 471 | + dash_dcc.wait_for_text_to_equal("#btn-1", "changed 1") |
| 472 | + assert spinners == [] |
| 473 | + |
| 474 | + assert dash_dcc.get_logs() == [] |
| 475 | + |
| 476 | + |
| 477 | +# update multiple props of same component, only targeted id/prop triggers spinner |
| 478 | +def test_ldcp011_loading_component_target_components(dash_dcc): |
| 479 | + lock = Lock() |
| 480 | + |
| 481 | + app = Dash(__name__) |
| 482 | + |
| 483 | + app.layout = html.Div( |
| 484 | + [ |
| 485 | + dcc.Loading( |
| 486 | + [ |
| 487 | + html.Button(id="btn-1"), |
| 488 | + html.Button(id="btn-2"), |
| 489 | + html.Button(id="btn-3"), |
| 490 | + ], |
| 491 | + className="loading-1", |
| 492 | + target_components={"btn-1": "className"}, |
| 493 | + ) |
| 494 | + ], |
| 495 | + id="root", |
| 496 | + ) |
| 497 | + |
| 498 | + @app.callback(Output("btn-1", "children"), [Input("btn-2", "n_clicks")]) |
| 499 | + def updateDiv1(n_clicks): |
| 500 | + if n_clicks: |
| 501 | + with lock: |
| 502 | + return "changed 1" |
| 503 | + return "content 1" |
| 504 | + |
| 505 | + @app.callback(Output("btn-1", "className"), [Input("btn-3", "n_clicks")]) |
| 506 | + def updateDiv2(n_clicks): |
| 507 | + if n_clicks: |
| 508 | + with lock: |
| 509 | + return "new-class" |
| 510 | + return "" |
| 511 | + |
| 512 | + dash_dcc.start_server(app) |
| 513 | + |
| 514 | + dash_dcc.wait_for_text_to_equal("#btn-1", "content 1") |
| 515 | + |
| 516 | + with lock: |
| 517 | + dash_dcc.find_element("#btn-2").click() |
| 518 | + |
| 519 | + spinners = dash_dcc.find_elements(".loading-1 .dash-spinner") |
| 520 | + dash_dcc.wait_for_text_to_equal("#btn-1", "") |
| 521 | + dash_dcc.wait_for_text_to_equal("#btn-1", "changed 1") |
| 522 | + assert spinners == [] |
| 523 | + |
| 524 | + with lock: |
| 525 | + dash_dcc.find_element("#btn-3").click() |
| 526 | + |
| 527 | + dash_dcc.find_element(".loading-1 .dash-spinner") |
| 528 | + dash_dcc.wait_for_text_to_equal("#btn-1", "") |
| 529 | + |
| 530 | + dash_dcc.wait_for_class_to_equal("#btn-1", "new-class") |
| 531 | + |
| 532 | + assert dash_dcc.get_logs() == [] |
| 533 | + |
| 534 | + |
| 535 | +def test_ldcp012_loading_component_custom_spinner(dash_dcc): |
| 536 | + lock = Lock() |
| 537 | + |
| 538 | + app = Dash(__name__) |
| 539 | + |
| 540 | + app.layout = html.Div( |
| 541 | + [ |
| 542 | + dcc.Loading( |
| 543 | + [html.Div(id="div-1")], |
| 544 | + className="loading", |
| 545 | + custom_spinner=html.Div(id="my-spinner"), |
| 546 | + ) |
| 547 | + ], |
| 548 | + id="root", |
| 549 | + ) |
| 550 | + |
| 551 | + @app.callback(Output("div-1", "children"), [Input("root", "n_clicks")]) |
| 552 | + def updateDiv(n_clicks): |
| 553 | + if n_clicks: |
| 554 | + with lock: |
| 555 | + return "changed" |
| 556 | + return "content" |
| 557 | + |
| 558 | + with lock: |
| 559 | + dash_dcc.start_server(app) |
| 560 | + dash_dcc.wait_for_text_to_equal("#div-1", "content") |
| 561 | + |
| 562 | + dash_dcc.find_element("#root").click() |
| 563 | + |
| 564 | + dash_spinner = dash_dcc.find_elements(".loading .dash-spinner") |
| 565 | + dash_dcc.find_element("#my-spinner") |
| 566 | + # mounted but hidden, so looks like no text |
| 567 | + dash_dcc.wait_for_text_to_equal("#div-1", "") |
| 568 | + |
| 569 | + dash_dcc.wait_for_text_to_equal("#div-1", "changed") |
| 570 | + assert dash_spinner == [] |
| 571 | + |
| 572 | + assert dash_dcc.get_logs() == [] |
| 573 | + |
| 574 | + |
| 575 | +def test_ldcp013_loading_component_display_show(dash_dcc): |
| 576 | + |
| 577 | + app = Dash(__name__) |
| 578 | + |
| 579 | + app.layout = html.Div( |
| 580 | + [ |
| 581 | + dcc.Loading( |
| 582 | + [html.Div("content", id="div-1")], className="loading", display="show" |
| 583 | + ) |
| 584 | + ], |
| 585 | + id="root", |
| 586 | + ) |
| 587 | + dash_dcc.start_server(app) |
| 588 | + |
| 589 | + dash_dcc.find_elements(".loading .dash-spinner") |
| 590 | + # mounted but hidden, so looks like no text |
| 591 | + dash_dcc.wait_for_text_to_equal("#div-1", "") |
| 592 | + |
| 593 | + assert dash_dcc.get_logs() == [] |
| 594 | + |
| 595 | + |
| 596 | +# Same as ldcp002, but with the display="hide", the spinner should not show |
| 597 | +def test_ldcp014_loading_component_delay_hide(dash_dcc): |
| 598 | + lock = Lock() |
| 599 | + |
| 600 | + app = Dash(__name__) |
| 601 | + |
| 602 | + app.layout = html.Div( |
| 603 | + [dcc.Loading([html.Div(id="div-1")], className="loading", display="hide")], |
| 604 | + id="root", |
| 605 | + ) |
| 606 | + |
| 607 | + @app.callback(Output("div-1", "children"), [Input("root", "n_clicks")]) |
| 608 | + def updateDiv(n_clicks): |
| 609 | + if n_clicks: |
| 610 | + with lock: |
| 611 | + return "changed" |
| 612 | + return "content" |
| 613 | + |
| 614 | + with lock: |
| 615 | + dash_dcc.start_server(app) |
| 616 | + dash_dcc.wait_for_text_to_equal("#div-1", "content") |
| 617 | + |
| 618 | + dash_dcc.find_element("#root").click() |
| 619 | + |
| 620 | + spinners = dash_dcc.find_elements(".loading .dash-spinner") |
| 621 | + |
| 622 | + dash_dcc.wait_for_text_to_equal("#div-1", "changed") |
| 623 | + assert spinners == [] |
| 624 | + |
| 625 | + assert dash_dcc.get_logs() == [] |
| 626 | + |
| 627 | + |
| 628 | +# Same as ldcp002, but with the delay, the spinner should not show |
| 629 | +def test_ldcp015_loading_component_delay_show(dash_dcc): |
| 630 | + lock = Lock() |
| 631 | + |
| 632 | + app = Dash(__name__) |
| 633 | + |
| 634 | + app.layout = html.Div( |
| 635 | + [dcc.Loading([html.Div(id="div-1")], className="loading", delay_show=2500)], |
| 636 | + id="root", |
| 637 | + ) |
| 638 | + |
| 639 | + @app.callback(Output("div-1", "children"), [Input("root", "n_clicks")]) |
| 640 | + def updateDiv(n_clicks): |
| 641 | + if n_clicks: |
| 642 | + with lock: |
| 643 | + return "changed" |
| 644 | + return "content" |
| 645 | + |
| 646 | + with lock: |
| 647 | + dash_dcc.start_server(app) |
| 648 | + dash_dcc.wait_for_text_to_equal("#div-1", "content") |
| 649 | + |
| 650 | + dash_dcc.find_element("#root").click() |
| 651 | + |
| 652 | + spinners = dash_dcc.find_elements(".loading .dash-spinner") |
| 653 | + # mounted but hidden, so looks like no text |
| 654 | + dash_dcc.wait_for_text_to_equal("#div-1", "") |
| 655 | + |
| 656 | + dash_dcc.wait_for_text_to_equal("#div-1", "changed") |
| 657 | + assert spinners == [] |
| 658 | + |
| 659 | + assert dash_dcc.get_logs() == [] |
| 660 | + |
| 661 | + |
| 662 | +def test_ldcp016_loading_component_delay_hide(dash_dcc): |
| 663 | + lock = Lock() |
| 664 | + |
| 665 | + app = Dash(__name__) |
| 666 | + |
| 667 | + app.layout = html.Div( |
| 668 | + [dcc.Loading([html.Div(id="div-1")], className="loading", delay_hide=300)], |
| 669 | + id="root", |
| 670 | + ) |
| 671 | + |
| 672 | + @app.callback(Output("div-1", "children"), [Input("root", "n_clicks")]) |
| 673 | + def updateDiv(n_clicks): |
| 674 | + if n_clicks: |
| 675 | + with lock: |
| 676 | + return "changed" |
| 677 | + return "content" |
| 678 | + |
| 679 | + with lock: |
| 680 | + dash_dcc.start_server(app) |
| 681 | + dash_dcc.wait_for_text_to_equal("#div-1", "content") |
| 682 | + |
| 683 | + dash_dcc.find_element("#root").click() |
| 684 | + dash_dcc.find_element(".loading .dash-spinner") |
| 685 | + |
| 686 | + time.sleep(0.2) |
| 687 | + dash_dcc.find_element(".loading .dash-spinner") |
| 688 | + dash_dcc.wait_for_text_to_equal("#div-1", "changed") |
| 689 | + |
| 690 | + assert dash_dcc.get_logs() == [] |
0 commit comments