【问题标题】:How to adapt height of a div in dash (python)?如何在破折号(python)中调整div的高度?
【发布时间】:2023-01-12 07:06:44
【问题描述】:

我使用下面的应用程序并希望使用 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)

【问题讨论】:

  • 你能试试'height': '200'而不是'height': '200%'吗?
  • @hoatran 也没有效果
  • 那你想得到什么?您想要散点图 div 的高度与两条线图相同吗?
  • 也许此文档中的示例可以帮助您:https://dash.plotly.com/interactive-graphing?_gl=1*da79nm*_ga*MTM0MTQ3MDU2My4xNjU0MjQwMTM1*_ga_6G7EE0JNSC*MTY3MzQzMDc0Ny40MTcuMS4xNjczNDMzNDEwLjAuMC4w
  • @hoatran 我想改变 div div_g 的高度

标签: python plotly-dash


【解决方案1】:

以像素为单位指定高度。

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

如果您要为高度使用百分比值,那么您的 div 需要位于另一个具有特定高度的 div 内,并且父级不能有 display=flex 否则百分比将不起作用。参见w3 displayw3 position

如果你真的想使用百分比,那么你可以设置 position=absolutediv_g 的样式,但你还必须指定顶部/左侧位置。

参考w3 height property

【讨论】:

    猜你喜欢
    • 2021-10-13
    • 2021-10-06
    • 1970-01-01
    • 1970-01-01
    • 2015-09-19
    • 2013-10-09
    • 2013-10-01
    • 2014-03-08
    • 1970-01-01
    相关资源
    最近更新 更多