dash中的buffer size怎么获取
时间: 2024-03-06 08:52:10 浏览: 28
在Dash中,可以通过`dcc.Graph`的`relayoutData`属性获取用户交互事件的信息,其中包括`autosize`和`bns`两个属性。`autosize`表示是否自动调整图表大小,`bns`表示plotly.js中的buffer size大小。可以通过以下代码来获取`bns`的值:
```python
import dash
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output
app = dash.Dash(__name__)
app.layout = html.Div([
dcc.Graph(
id='example-graph',
figure={
'data': [
{'x': [1, 2, 3], 'y': [4, 1, 2], 'type': 'bar', 'name': 'SF'},
{'x': [1, 2, 3], 'y': [2, 4, 5], 'type': 'bar', 'name': 'Montreal'},
],
'layout': {
'title': 'Dash Data Visualization'
}
}
),
html.Div(id='output')
])
@app.callback(Output('output', 'children'), [Input('example-graph', 'relayoutData')])
def show_relayout_data(relayout_data):
bns = relayout_data.get('bns', None)
if bns is not None:
return f"Buffer size is {bns}"
else:
return "Buffer size is not available"
if __name__ == '__main__':
app.run_server(debug=True)
```
上面的示例中,使用`Input('example-graph', 'relayoutData')`来监听用户交互事件,在回调函数中获取`bns`的值并显示在页面中。