这应该只是在文本前面放置并使div内联块之类的问题。
希望看起来像“ DATE:”。使用html.Div失败,即使DATE位于第二分区内,也将其放在单独的行上。
from datetime import datetime as dt
import dash
import dash_html_components as html
import dash_core_components as dcc
import re
external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css']
app = dash.Dash(__name__, external_stylesheets=external_stylesheets)
app.layout = html.Div([
dcc.DatePickerRange(
id='my-date-picker-range',
min_date_allowed=dt(1995, 8, 5),
max_date_allowed=dt(2017, 9, 19),
initial_visible_month=dt(2017, 8, 5),
end_date=dt(2017, 8, 25).date()
),
html.Div(id='output-container-date-picker-range')
])
@app.callback(
dash.dependencies.Output('output-container-date-picker-range', 'children'),
[dash.dependencies.Input('my-date-picker-range', 'start_date'),
dash.dependencies.Input('my-date-picker-range', 'end_date')])
def update_output(start_date, end_date):
string_prefix = 'You have selected: '
if start_date is not None:
start_date = dt.strptime(re.split('T| ', start_date)[0], '%Y-%m-%d')
start_date_string = start_date.strftime('%B %d, %Y')
string_prefix = string_prefix + 'Start Date: ' + start_date_string + ' | '
if end_date is not None:
end_date = dt.strptime(re.split('T| ', end_date)[0], '%Y-%m-%d')
end_date_string = end_date.strftime('%B %d, %Y')
string_prefix = string_prefix + 'End Date: ' + end_date_string
if len(string_prefix) == len('You have selected: '):
return 'Select a date to see it displayed here'
else:
return string_prefix
if __name__ == '__main__':
app.run_server(debug=True)
此布局适合您吗?
app.layout = html.Div([
html.Div(["DATE: ",
dcc.DatePickerRange(
id='my-date-picker-range',
min_date_allowed=dt(1995, 8, 5),
max_date_allowed=dt(2017, 9, 19),
initial_visible_month=dt(2017, 8, 5),
end_date=dt(2017, 8, 25).date()
)],
style={'width': '49%', 'display': 'inline-block'}),
html.Div(id='output-container-date-picker-range')
])
技巧是添加另一个具有两个元素和Div
样式的inline-block
。