簡體   English   中英

如何在破折號(python)中調整div的高度?

[英]How to adapt height of a div in dash (python)?

我使用下面的應用程序並希望使用 id = "change-height" 調整 div 的高度。 為此,我在樣式參數中添加了“高度”參數。

div_g = html.Div([g_scatter]
    , id = "change-height"
    , style={'width': '49%', 'display': 'inline-block', 'height': '200%'}
    )

但是高度值沒有影響。 但是,如果我更改寬度參數,它就會生效。 如何調整 div div_g的高度? 我可以將高度設置為與 div div_xy的高度相同的值嗎?

from dash import Dash, html, dcc, Input, Output
import pandas as pd
import plotly.express as px

external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css']

app = Dash(__name__, external_stylesheets=external_stylesheets)

df = pd.read_csv('https://plotly.github.io/datasets/country_indicators.csv')

dd_1 = dcc.Dropdown(
                df['Indicator Name'].unique(),
                'Fertility rate, total (births per woman)',
                id='crossfilter-xaxis-column',
            )
dd_2 = dcc.Dropdown(
                df['Indicator Name'].unique(),
                'Life expectancy at birth, total (years)',
                id='crossfilter-yaxis-column'
            )
ri_1 = dcc.RadioItems(
                ['Linear', 'Log'],
                'Linear',
                id='crossfilter-xaxis-type',
                labelStyle={'display': 'inline-block', 'marginTop': '5px'}
            )
ri_2 = dcc.RadioItems(
                ['Linear', 'Log'],
                'Linear',
                id='crossfilter-yaxis-type',
                labelStyle={'display': 'inline-block', 'marginTop': '5px'}
            )

gx = dcc.Graph(id='x-time-series')
gy = dcc.Graph(id='y-time-series')

div_dd = html.Div([dd_1, dd_2])

sl = dcc.Slider(
        df['Year'].min(),
        df['Year'].max(),
        step=None,
        id='crossfilter-year--slider',
        value=df['Year'].max(),
        marks={str(year): str(year) for year in df['Year'].unique()}
    )

div_xy = html.Div([ri_1,gx,ri_2,gy,sl]
, style={'display': 'inline-block','width': '49%'})

g_scatter = dcc.Graph(
            id='crossfilter-indicator-scatter',
            hoverData={'points': [{'customdata': 'Japan'}]}
        )

div_g = html.Div([g_scatter]
    , id = "change-height"
    , style={'width': '49%', 'display': 'inline-block', 'height': '200%'}
    )

div_main = html.Div(
    [div_xy,div_g]
    ,style={"display": "flex"}
    )

app.layout = html.Div(
    [
      div_dd
    , div_main
    ]
    )


@app.callback(
    Output('crossfilter-indicator-scatter', 'figure'),
    Input('crossfilter-xaxis-column', 'value'),
    Input('crossfilter-yaxis-column', 'value'),
    Input('crossfilter-xaxis-type', 'value'),
    Input('crossfilter-yaxis-type', 'value'),
    Input('crossfilter-year--slider', 'value'))
def update_graph(xaxis_column_name, yaxis_column_name,
                 xaxis_type, yaxis_type,
                 year_value):
    dff = df[df['Year'] == year_value]

    fig = px.scatter(x=dff[dff['Indicator Name'] == xaxis_column_name]['Value'],
            y=dff[dff['Indicator Name'] == yaxis_column_name]['Value'],
            hover_name=dff[dff['Indicator Name'] == yaxis_column_name]['Country Name']
            )

    fig.update_traces(customdata=dff[dff['Indicator Name'] == yaxis_column_name]['Country Name'])

    fig.update_xaxes(title=xaxis_column_name, type='linear' if xaxis_type == 'Linear' else 'log')

    fig.update_yaxes(title=yaxis_column_name, type='linear' if yaxis_type == 'Linear' else 'log')

    fig.update_layout(margin={'l': 40, 'b': 40, 't': 10, 'r': 0}, hovermode='closest')

    return fig


def create_time_series(dff, axis_type, title):

    fig = px.scatter(dff, x='Year', y='Value')

    fig.update_traces(mode='lines+markers')

    fig.update_xaxes(showgrid=False)

    fig.update_yaxes(type='linear' if axis_type == 'Linear' else 'log')

    fig.add_annotation(x=0, y=0.85, xanchor='left', yanchor='bottom',
                       xref='paper', yref='paper', showarrow=False, align='left',
                       text=title)

    fig.update_layout(height=225, margin={'l': 20, 'b': 30, 'r': 10, 't': 10})

    return fig


@app.callback(
    Output('x-time-series', 'figure'),
    Input('crossfilter-indicator-scatter', 'hoverData'),
    Input('crossfilter-xaxis-column', 'value'),
    Input('crossfilter-xaxis-type', 'value'))
def update_y_timeseries(hoverData, xaxis_column_name, axis_type):
    country_name = hoverData['points'][0]['customdata']
    dff = df[df['Country Name'] == country_name]
    dff = dff[dff['Indicator Name'] == xaxis_column_name]
    title = '<b>{}</b><br>{}'.format(country_name, xaxis_column_name)
    return create_time_series(dff, axis_type, title)


@app.callback(
    Output('y-time-series', 'figure'),
    Input('crossfilter-indicator-scatter', 'hoverData'),
    Input('crossfilter-yaxis-column', 'value'),
    Input('crossfilter-yaxis-type', 'value'))
def update_x_timeseries(hoverData, yaxis_column_name, axis_type):
    dff = df[df['Country Name'] == hoverData['points'][0]['customdata']]
    dff = dff[dff['Indicator Name'] == yaxis_column_name]
    return create_time_series(dff, axis_type, yaxis_column_name)


if __name__ == '__main__':
    app.run_server(debug=True)

以像素為單位指定高度。

div_g = html.Div([g_scatter]
    , id = "change-height"
    , style={'width': '49%', 'display': 'inline-block', 'height': '200px'}
    )

如果您要為高度使用百分比值,那么您的 div 需要位於另一個具有特定高度的 div 內,並且父級不能有display=flex否則百分比將不起作用。 請參見w3 顯示w3 position

如果你真的想使用百分比那么你可以在div_g的樣式中設置position=absolute但你還必須指定 top/left position。

請參閱w3 height 屬性以供參考

編輯

通過執行我上面所述的操作,指定的div高度會發生變化,但您無法分辨,因為div內的圖形保持不變。 如果你想改變圖形高度,你必須通過圖形回調中的fig.update_layout來實現。

fig.update_layout(height=200, margin={'l': 40, 'b': 40, 't': 10, 'r': 0}, hovermode='closest')

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM