【发布时间】:2021-10-02 05:27:26
【问题描述】:
我已成功地将 Dash 应用程序嵌入到我的 Django 应用程序中。我可以显示简单的图。但是我想要做的,并且无法通过以下文档来做,是将一个 pk id 变量传递到 Dash 应用程序中,根据该 pk 提取相关的数据库信息,然后绘制它。
我的 urls.py:
urlpatterns = [
url(r'^$', views.home, name='home'),
url(r'^model/(?P<pk>\d+)/$', views.model, name='model'),
views.py:
def home(request):
return render(request, 'home.html')
def model(request, pk):
context = {'data' : {'pk': pk}}
return render(request, 'model_results.html', context)
models.html 模板:
{%load plotly_dash%}
<body>
<div class="container">
<!--Row with two equal columns-->
<div class="row">
<div class="col-lg-1 col-md-1 col-xl-1 col-sm-1">
<div class = "card">
<div class = "card-body">
{%plotly_app name="tutorial_1" initial_arguments=data%}
</div>
</div>
</div>
</div>
</div>
</body>
最后,我的仪表板应用程序:
app = DjangoDash('tutorial_1')
app.layout = html.Div(children=[
html.H1(children='Dash Tutorials'),
dcc.Graph(
id='example',
figure={
'data': [
{'x': [1, 2, 3, 4, 5], 'y': [9, 6, 2, 1, 5], 'type': 'line', 'name': 'Boats'},
{'x': [1, 2, 3, 4, 5], 'y': [8, 7, 2, 7, 3], 'type': 'bar', 'name': 'Cars'},
],
'layout': {
'title': 'Basic Dash Example'
}
}
)
])
if __name__ == '__main__':
app.run_server(debug=True)
从其他示例中我不清楚如何使用 Django ORM 通过在 Dash 应用程序中包含一些内容(例如 doc_obj = model_example.objects.get(pk = pk))来提取所需的信息。
【问题讨论】:
标签: python django plotly-dash