Skip to content

Commit df2aeea

Browse files
committed
Deprate components on init.
1 parent 092464f commit df2aeea

File tree

3 files changed

+35
-61
lines changed

3 files changed

+35
-61
lines changed
Lines changed: 14 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,30 @@
1-
from dash.exceptions import PreventUpdate
21
from dash import Dash, Input, Output, dcc, html
3-
import flask
42
import pytest
53
import time
64

75

86
@pytest.mark.parametrize("add_initial_logout_button", [False, True])
97
def test_llgo001_location_logout(dash_dcc, add_initial_logout_button):
8+
# FIXME: Logout button is deprecated, remove this test for dash 3.0
109
app = Dash(__name__)
1110

12-
@app.server.route("/_logout", methods=["POST"])
13-
def on_logout():
14-
rep = flask.redirect("/logged-out")
15-
rep.set_cookie("logout-cookie", "", 0)
16-
return rep
17-
18-
layout_children = [
19-
html.H2("Logout test"),
20-
dcc.Location(id="location"),
21-
html.Div(id="content"),
22-
]
23-
if add_initial_logout_button:
24-
layout_children.append(dcc.LogoutButton())
25-
app.layout = html.Div(layout_children)
26-
27-
@app.callback(Output("content", "children"), [Input("location", "pathname")])
28-
def on_location(location_path):
29-
if location_path is None:
30-
raise PreventUpdate
31-
32-
if "logged-out" in location_path:
33-
return "Logged out"
34-
else:
35-
36-
@flask.after_this_request
37-
def _insert_cookie(rep):
38-
rep.set_cookie("logout-cookie", "logged-in")
39-
return rep
40-
41-
return dcc.LogoutButton(id="logout-btn", logout_url="/_logout")
42-
4311
with pytest.warns(
4412
DeprecationWarning,
4513
match="The Logout Button is no longer used with Dash Enterprise and can be replaced with a html.Button or html.A.",
4614
):
47-
dash_dcc.start_server(app)
48-
time.sleep(1)
49-
dash_dcc.percy_snapshot("Core Logout button")
50-
51-
assert dash_dcc.driver.get_cookie("logout-cookie")["value"] == "logged-in"
15+
app.layout = [
16+
html.H2("Logout test"),
17+
html.Div(id="content"),
18+
]
19+
if add_initial_logout_button:
20+
app.layout.append(dcc.LogoutButton())
21+
else:
5222

53-
dash_dcc.wait_for_element("#logout-btn").click()
54-
dash_dcc.wait_for_text_to_equal("#content", "Logged out")
23+
@app.callback(Output("content", "children"), Input("content", "id"))
24+
def on_location(location_path):
25+
return dcc.LogoutButton(id="logout-btn", logout_url="/_logout")
5526

56-
assert not dash_dcc.driver.get_cookie("logout-cookie")
27+
dash_dcc.start_server(app)
28+
time.sleep(1)
5729

58-
assert dash_dcc.get_logs() == []
30+
assert dash_dcc.get_logs() == []

dash/_validate.py

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
import sys
2-
from collections import defaultdict
32
from collections.abc import MutableSequence
43
import re
5-
import warnings
64
from textwrap import dedent
75
from keyword import iskeyword
86
import flask
@@ -424,21 +422,6 @@ def validate_layout(layout, layout_value):
424422
component_ids = set()
425423

426424
def _validate(value):
427-
def _validate_type(comp):
428-
deprecated_components = defaultdict(lambda: defaultdict(dict))
429-
deprecated_components["dash_core_components"][
430-
"LogoutButton"
431-
] = """
432-
The Logout Button is no longer used with Dash Enterprise and can be replaced with a html.Button or html.A.
433-
eg: html.A(href=os.getenv('DASH_LOGOUT_URL'))
434-
"""
435-
436-
_type = getattr(comp, "_type", "")
437-
_ns = getattr(comp, "_namespace", "")
438-
deprecation_message = deprecated_components[_ns][_type]
439-
if deprecation_message:
440-
warnings.warn(dedent(deprecation_message), DeprecationWarning)
441-
442425
def _validate_id(comp):
443426
component_id = stringify_id(getattr(comp, "id", None))
444427
if component_id and component_id in component_ids:
@@ -449,11 +432,9 @@ def _validate_id(comp):
449432
)
450433
component_ids.add(component_id)
451434

452-
_validate_type(value)
453435
_validate_id(value)
454436

455437
for component in value._traverse(): # pylint: disable=protected-access
456-
_validate_type(component)
457438
_validate_id(component)
458439

459440
if isinstance(layout_value, (list, tuple)):

dash/development/base_component.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,26 @@
44
import sys
55
import uuid
66
import random
7+
import warnings
8+
import textwrap
79

810
from .._utils import patch_collections_abc, stringify_id, OrderedSet
911

1012
MutableSequence = patch_collections_abc("MutableSequence")
1113

1214
rd = random.Random(0)
1315

16+
_deprecated_components = {
17+
"dash_core_components": {
18+
"LogoutButton": textwrap.dedent(
19+
"""
20+
The Logout Button is no longer used with Dash Enterprise and can be replaced with a html.Button or html.A.
21+
eg: html.A(href=os.getenv('DASH_LOGOUT_URL'))
22+
"""
23+
)
24+
}
25+
}
26+
1427

1528
# pylint: disable=no-init,too-few-public-methods
1629
class ComponentRegistry:
@@ -95,6 +108,7 @@ def __str__(self):
95108
REQUIRED = _REQUIRED()
96109

97110
def __init__(self, **kwargs):
111+
self._validate_deprecation()
98112
import dash # pylint: disable=import-outside-toplevel, cyclic-import
99113

100114
# pylint: disable=super-init-not-called
@@ -405,6 +419,13 @@ def __repr__(self):
405419
props_string = repr(getattr(self, "children", None))
406420
return f"{self._type}({props_string})"
407421

422+
def _validate_deprecation(self):
423+
_type = getattr(self, "_type", "")
424+
_ns = getattr(self, "_namespace", "")
425+
deprecation_message = _deprecated_components.get(_ns, {}).get(_type)
426+
if deprecation_message:
427+
warnings.warn(DeprecationWarning(textwrap.dedent(deprecation_message)))
428+
408429

409430
def _explicitize_args(func):
410431
# Python 2

0 commit comments

Comments
 (0)