def test_dtps010_local_and_session_persistence(dash_dcc): app = Dash(__name__) app.layout = html.Div([ dcc.DatePickerSingle(id="dps-local", persistence=True, day_size=47), dcc.DatePickerSingle( id="dps-session", persistence=True, persistence_type="session", day_size=47, ), ]) dash_dcc.start_server(app) assert not dash_dcc.find_element("#dps-local input").get_attribute( "value") and not dash_dcc.find_element( "#dps-session input").get_attribute( "value"), "component should contain no initial date" for idx in range(3): local = dash_dcc.select_date_single("dps-local", index=idx) session = dash_dcc.select_date_single("dps-session", index=idx) dash_dcc.wait_for_page() assert ( dash_dcc.find_element("#dps-local input").get_attribute("value") == local and dash_dcc.find_element("#dps-session input").get_attribute("value") == session), "the date value should be consistent after refresh" assert dash_dcc.get_logs() == []
def cb(clicks): if clicks is None: return no_update if clicks % 2 == 1: return [ dcc.DatePickerSingle( id="dps-memory", min_date_allowed=datetime(2010, 1, 1), max_date_allowed=datetime(2099, 12, 31), initial_visible_month=datetime.today().date() - timedelta(days=1), persistence=True, persistence_type="memory", day_size=47, ), dcc.DatePickerSingle( id="dps-none", min_date_allowed=datetime(2010, 1, 1), max_date_allowed=datetime(2099, 12, 31), initial_visible_month=datetime.today().date() - timedelta(days=1), day_size=47, ), ] else: return "switched"
def test_dtps013_disabled_days_arent_clickable(dash_dcc): app = Dash(__name__) app.layout = html.Div( [ html.Label("Operating Date"), dcc.DatePickerSingle( id="dps", min_date_allowed=datetime(2021, 1, 1), max_date_allowed=datetime(2021, 1, 31), initial_visible_month=datetime(2021, 1, 1), disabled_days=[datetime(2021, 1, 10)], ), ], style={ "width": "10%", "display": "inline-block", "marginLeft": 10, "marginRight": 10, "marginBottom": 10, }, ) dash_dcc.start_server(app) date = dash_dcc.find_element("#dps input") assert not date.get_attribute("value") assert not dash_dcc.select_date_single( "dps", day=10), "Disabled days should not be clickable" assert dash_dcc.select_date_single("dps", day=1), "Other days should be clickable" # open datepicker to take snapshot date.click() dash_dcc.percy_snapshot("dtps013 - disabled days")
def test_dtps0014_disabed_days_timeout(dash_dcc): app = Dash(__name__) min_date = pd.to_datetime("2010-01-01") max_date = pd.to_datetime("2099-01-01") disabled_days = [ x for x in pd.date_range(min_date, max_date, freq="D") if x.day != 1 ] app.layout = html.Div([ html.Label("Operating Date"), dcc.DatePickerSingle( id="dps", min_date_allowed=min_date, max_date_allowed=max_date, disabled_days=disabled_days, ), ]) dash_dcc.start_server(app) date = dash_dcc.wait_for_element("#dps", timeout=5) """ WebDriver click() function hangs at the time of the react code execution, so it necessary to check execution time. """ start_time = time.time() date.click() assert time.time() - start_time < 5 dash_dcc.wait_for_element(".SingleDatePicker_picker", timeout=5) assert dash_dcc.get_logs() == []
def test_dtps001_simple_click(dash_dcc): app = Dash(__name__) app.layout = html.Div( [ html.Label("Operating Date"), dcc.DatePickerSingle( id="dps", min_date_allowed=datetime(2010, 1, 1), max_date_allowed=datetime(2099, 12, 31), initial_visible_month=datetime.today().date() - timedelta(days=1), day_size=47, ), ], style={ "width": "10%", "display": "inline-block", "marginLeft": 10, "marginRight": 10, "marginBottom": 10, }, ) dash_dcc.start_server(app) date = dash_dcc.find_element("#dps input") assert not date.get_attribute("value") assert dash_dcc.select_date_single( "dps", index=3), "Component should be clickable to choose a valid date" assert dash_dcc.get_logs() == []
def test_cdpr001_date_clearable_true_works(dash_dcc): app = Dash(__name__) app.layout = html.Div( [ dcc.DatePickerRange(id="dpr", clearable=True), dcc.DatePickerSingle(id="dps", clearable=True), ] ) dash_dcc.start_server(app) # DPR start_date, end_date = dash_dcc.select_date_range("dpr", (1, 28)) close_btn = dash_dcc.wait_for_element('button[aria-label="Clear Dates"]') assert ( "1" in start_date and "28" in end_date ), "both start date and end date should match the selected day" close_btn.click() start_date, end_date = dash_dcc.get_date_range("dpr") assert not start_date and not end_date, "both start and end dates should be cleared" # DPS selected = dash_dcc.select_date_single("dps", day="1") assert selected, "single date should get a value" close_btn = dash_dcc.wait_for_element("#dps button") close_btn.click() (single_date,) = dash_dcc.get_date_range("dps") assert not single_date, "date should be cleared" assert dash_dcc.get_logs() == []
def update_output(value): return dcc.DatePickerSingle( id="dps", min_date_allowed=datetime(2020, 1, 1), max_date_allowed=datetime(2020, 1, 7), date=datetime(2020, 1, 3, 1, 1, 1, value), persistence=True, persistence_type="session", )
def dateDiv(self): date_input = dcc.DatePickerSingle( id=self.date_div_date_id, date=datetime.date( year=2019, month=9, day=1 ), # TODO: start at begining of user's data date # date = datetime.datetime.today()-relativedelta(months=5), display_format="D/M/Y", ) return date_input
def display_data_retrieval(value, tab): global client global client_thread if value == 'realtime': if tab == 'server': delete_residual_data(delete_streams=False) try: client.data_retrieval = False client_thread.close() client_thread = SLThread('Client SL Realtime', client) client_thread.start() except AttributeError: pass return [] else: if tab == 'server': try: client.data_retrieval = True client_thread.close() except AttributeError: pass return [dcc.DatePickerSingle(id='date-picker-dcc', date=UTCDateTime().date, display_format='YYYY/MM/DD', number_of_months_shown=2, style={'display': 'inline-block', 'width': '40%'}), dbc.Input(id="input-hour", type="number", placeholder="HH", min=0, max=23, step=1, style={'display': 'inline-block', 'width': '20%'}), dbc.Input(id="input-min", type="number", placeholder="MM", min=0, max=59, step=1, style={'display': 'inline-block', 'width': '20%'}), dbc.Input(id="input-sec", type="number", placeholder="SS", min=0, max=59, step=1, style={'display': 'inline-block', 'width': '20%'}), html.Br(), 'Duration: ', dbc.Input(id="input-period", type="number", value=1, min=1, max=59, step=1, style={'display': 'inline-block', 'width': "20%"}), dbc.Select(id='select-period', options=[ {"label": "day", "value": "day"}, {"label": "hour", "value": "hour"}, {"label": "min", "value": "min"}, {"label": "sec", "value": "sec"}, ], value='hour', style={'display': 'inline-block', 'width':"20%"}), ' ', dbc.Button("Retrieve Data", id='retrieve-data-btn', color="success", className="mb-1", n_clicks=0, style={'display': 'inline-block'}), html.Br()]
def test_dtps012_initial_month(dash_dcc): app = Dash(__name__) app.layout = html.Div([ dcc.DatePickerSingle( id="dps-initial-month", min_date_allowed=datetime(2010, 1, 1), max_date_allowed=datetime(2099, 12, 31), ) ]) dash_dcc.start_server(app) date_picker = dash_dcc.find_element("#dps-initial-month") date_picker.click() dash_dcc.wait_for_text_to_equal( "#dps-initial-month .CalendarMonth.CalendarMonth_1[data-visible=true] strong", "January 2010", ) assert dash_dcc.get_logs() == []
def platter_app(): app = Dash(__name__) app.layout = html.Div([ html.Div(id="waitfor"), html.Label("Upload"), dcc.Upload(), html.Label("Horizontal Tabs"), dcc.Tabs( id="tabs", children=[ dcc.Tab( label="Tab one", className="test", style={"border": "1px solid magenta"}, children=[html.Div(["Test"])], ), dcc.Tab( label="Tab two", children=[ html.Div([ html.H1("This is the content in tab 2"), html.P("A graph here would be nice!"), ]) ], id="tab-one", ), dcc.Tab( label="Tab three", children=[ html.Div([html.H1("This is the content in tab 3")]) ], ), ], style={"fontFamily": "system-ui"}, content_style={ "border": "1px solid #d6d6d6", "padding": "44px" }, parent_style={ "maxWidth": "1000px", "margin": "0 auto" }, ), html.Label("Vertical Tabs"), dcc.Tabs( id="tabs1", vertical=True, children=[ dcc.Tab(label="Tab one", children=[html.Div(["Test"])]), dcc.Tab( label="Tab two", children=[ html.Div([ html.H1("This is the content in tab 2"), html.P("A graph here would be nice!"), ]) ], ), dcc.Tab( label="Tab three", children=[ html.Div([html.H1("This is the content in tab 3")]) ], ), ], ), html.Label("Dropdown"), dcc.Dropdown(options=OPTIONS, value="MTL", id="dropdown"), html.Label("Multi-Select Dropdown"), dcc.Dropdown(options=OPTIONS, value=["MTL", "SF"], multi=True), html.Label("Radio Items"), dcc.RadioItems(options=OPTIONS, value="MTL"), html.Label("Checkboxes"), dcc.Checklist(options=OPTIONS, value=["MTL", "SF"]), html.Label("Text Input"), dcc.Input(value="", placeholder="type here", id="textinput"), html.Label("Disabled Text Input"), dcc.Input( value="disabled", type="text", id="disabled-textinput", disabled=True, ), html.Label("Slider"), dcc.Slider( min=0, max=9, marks={i: f"Label {i}" if i == 1 else str(i) for i in range(1, 6)}, value=5, ), html.Label("Graph"), dcc.Graph( id="graph", figure={ "data": [{ "x": [1, 2, 3], "y": [4, 1, 4] }], "layout": { "title": "北京" }, }, ), html.Div([ html.Label("DatePickerSingle"), dcc.DatePickerSingle(id="date-picker-single", date=datetime(1997, 5, 10)), html.Div( [ html.Label("DatePickerSingle - empty input"), dcc.DatePickerSingle(), ], id="dt-single-no-date-value", ), html.Div( [ html.Label( "DatePickerSingle - initial visible month (May 97)"), dcc.DatePickerSingle( initial_visible_month=datetime(1997, 5, 10)), ], id="dt-single-no-date-value-init-month", ), ]), html.Div([ html.Label("DatePickerRange"), dcc.DatePickerRange( id="date-picker-range", start_date_id="startDate", end_date_id="endDate", start_date=datetime(1997, 5, 3), end_date_placeholder_text="Select a date!", ), html.Div( [ html.Label("DatePickerRange - empty input"), dcc.DatePickerRange( start_date_id="startDate", end_date_id="endDate", start_date_placeholder_text="Start date", end_date_placeholder_text="End date", ), ], id="dt-range-no-date-values", ), html.Div( [ html.Label( "DatePickerRange - initial visible month (May 97)"), dcc.DatePickerRange( start_date_id="startDate", end_date_id="endDate", start_date_placeholder_text="Start date", end_date_placeholder_text="End date", initial_visible_month=datetime(1997, 5, 10), ), ], id="dt-range-no-date-values-init-month", ), ]), html.Label("TextArea"), dcc.Textarea(placeholder="Enter a value... 北京", style={"width": "100%"}), html.Label("Markdown"), dcc.Markdown(""" #### Dash and Markdown Dash supports [Markdown](https://rexxars.github.io/react-markdown/). Markdown is a simple way to write and format text. It includes a syntax for things like **bold text** and *italics*, [links](https://rexxars.github.io/react-markdown/), inline `code` snippets, lists, quotes, and more. 1. Links are auto-rendered: https://dash.plotly.com. 2. This uses ~commonmark~ GitHub flavored markdown. Tables are also supported: | First Header | Second Header | | ------------- | ------------- | | Content Cell | Content Cell | | Content Cell | Content Cell | 北京 """.replace(" ", "")), dcc.Markdown(["# Line one", "## Line two"]), dcc.Markdown(), dcc.Markdown(""" ```py import python print(3) ```"""), dcc.Markdown(["```py", "import python", "print(3)", "```"]), dcc.Markdown(), ]) yield app
def update_selection(click, patient, value): if value == 'M': month = ldd.month(rdb, patient) if click: value_m = click["points"][0]["x"][:7] else: value_m = month[0] drop_down = html.Div([dcc.Dropdown( style={'height': '40px'}, id={ 'type': 'filter-drop_down', 'index': 0 }, options=[{'label': name, 'value': name} for name in month], value=value_m, clearable=False )]) elif value == 'W': week = ldd.week(rdb, patient) if click: value_w = click["points"][0]["x"] else: value_w = week[0] drop_down = html.Div([dcc.Dropdown( style={'height': '40px'}, id={ 'type': 'filter-drop_down', 'index': 0 }, options=[{'label': name, 'value': name} for name in week], value=value_w, clearable=False )]) elif value == 'DOW': if click: value_dow = click["points"][0]["x"].replace(" ", "") else: value_dow = day_of_week[0] drop_down = html.Div([dcc.Dropdown( style={'height': '40px'}, id={ 'type': 'filter-drop_down', 'index': 1 }, options=[{'label': name, 'value': name} for name in day_of_week], value=value_dow, clearable=False )]) elif value == 'D': min_date, max_date = ldd.min_max_date(rdb,patient) if click: value_day = str(click["points"][0]["x"]) else: value_day = max_date drop_down = html.Div([dcc.DatePickerSingle( style={'height': '40px'}, id={ 'type': 'filter-drop_down', 'index': 0 }, min_date_allowed=min_date, max_date_allowed=max_date, display_format='D/M/Y', date=value_day)]) return drop_down
def to_date_input(): return dcc.DatePickerSingle(id='to-date-picker', date=str(dt.today()))
def from_date_input(): return dcc.DatePickerSingle(id='from-date-picker', date=str(dt(2000, 1, 1)))
def test_msps001_basic_persistence(dash_dcc): app = Dash(__name__) app.layout = html.Div([ dcc.Checklist( id="checklist", options=[ { "label": u"Slow 🐢", "value": u"🐢" }, { "label": u"Fast 🏎️", "value": u"🏎️" }, { "label": u"Faster 🚀", "value": u"🚀" }, ], value=[u"🏎️"], persistence=True, ), dcc.DatePickerRange( id="datepickerrange", start_date="2017-08-21", end_date="2024-04-08", start_date_id="start_date", end_date_id="end_date", initial_visible_month="2019-05-01", persistence=True, ), dcc.DatePickerSingle(id="datepickersingle", date="2019-01-01", persistence=True), dcc.Dropdown( id="dropdownsingle", options=[ { "label": u"One 1️⃣", "value": u"1️⃣" }, { "label": u"Two 2️⃣", "value": u"2️⃣" }, { "label": u"Three 3️⃣", "value": u"3️⃣" }, ], value=u"2️⃣", persistence=True, ), dcc.Dropdown( id="dropdownmulti", options=[ { "label": u"Four 4️⃣", "value": u"4️⃣" }, { "label": u"Five 5️⃣", "value": u"5️⃣" }, { "label": u"Six 6️⃣", "value": u"6️⃣" }, ], value=[u"4️⃣"], multi=True, persistence=True, ), dcc.Input(id="input", value="yes", persistence=True), dcc.RadioItems( id="radioitems", options=[ { "label": "Red", "value": "r" }, { "label": "Green", "value": "g" }, { "label": "Blue", "value": "b" }, ], value="b", persistence=True, ), dcc.RangeSlider(id="rangeslider", min=0, max=10, value=[3, 7], persistence=True), dcc.Slider(id="slider", min=20, max=30, value=25, persistence=True), dcc.Tabs( id="tabs", children=[ dcc.Tab(label="Eh?", children="Tab A", value="A"), dcc.Tab(label="Bee", children="Tab B", value="B"), dcc.Tab(label="Sea", children="Tab C", value="C"), ], value="A", persistence=True, ), dcc.Textarea(id="textarea", value="knock knock", persistence=True), html.Div(id="settings"), ]) @app.callback( Output("settings", "children"), [ Input("checklist", "value"), Input("datepickerrange", "start_date"), Input("datepickerrange", "end_date"), Input("datepickersingle", "date"), Input("dropdownsingle", "value"), Input("dropdownmulti", "value"), Input("input", "value"), Input("radioitems", "value"), Input("rangeslider", "value"), Input("slider", "value"), Input("tabs", "value"), Input("textarea", "value"), ], ) def make_output(*args): return json.dumps(args) initial_settings = [ [u"🏎️"], "2017-08-21", "2024-04-08", "2019-01-01", u"2️⃣", [u"4️⃣"], "yes", "b", [3, 7], 25, "A", "knock knock", ] dash_dcc.start_server(app) dash_dcc.wait_for_text_to_equal("#settings", json.dumps(initial_settings)) dash_dcc.find_element("#checklist label:last-child input").click() # 🚀 dash_dcc.select_date_range("datepickerrange", day_range=(4, )) dash_dcc.select_date_range("datepickerrange", day_range=(14, ), start_first=False) dash_dcc.find_element("#datepickersingle input").click() dash_dcc.select_date_single("datepickersingle", day="20") dash_dcc.find_element("#dropdownsingle .Select-input input").send_keys( "one" + Keys.ENTER) dash_dcc.find_element("#dropdownmulti .Select-input input").send_keys( "six" + Keys.ENTER) dash_dcc.find_element("#input").send_keys(" maybe") dash_dcc.find_element("#radioitems label:first-child input").click() # red range_slider = dash_dcc.find_element("#rangeslider") dash_dcc.click_at_coord_fractions(range_slider, 0.5, 0.25) # 5 dash_dcc.click_at_coord_fractions(range_slider, 0.8, 0.25) # 8 slider = dash_dcc.find_element("#slider") dash_dcc.click_at_coord_fractions(slider, 0.2, 0.25) # 22 dash_dcc.find_element("#tabs .tab:last-child").click() # C dash_dcc.find_element("#textarea").send_keys(Keys.ENTER + "who's there?") edited_settings = [ [u"🏎️", u"🚀"], "2019-05-04", "2019-05-14", "2019-01-20", u"1️⃣", [u"4️⃣", u"6️⃣"], "yes maybe", "r", [5, 8], 22, "C", "knock knock\nwho's there?", ] dash_dcc.wait_for_text_to_equal("#settings", json.dumps(edited_settings)) # now reload the page - all of these settings should persist dash_dcc.wait_for_page() dash_dcc.wait_for_text_to_equal("#settings", json.dumps(edited_settings)) assert dash_dcc.get_logs() == []