django通过模板标记使用plotly dash应用程序
django-plotly-dash的Python项目详细描述
django绘图短划线
将plotly dash应用程序公开为Django标记。多个Dash应用程序可以 然后嵌入到单个网页中,持久化并共享内部状态,还可以访问 当前用户和会话变量。
请在此处查看此项目的源: https://github.com/GibbsConsulting/django-plotly-dash
本自述文件提供了安装和使用软件包的简短指南,以及 概述如何运行演示应用程序。
更详细的信息 可以在在线文档中找到 https://readthedocs.org/projects/django-plotly-dash
演示的在线版本可以在https://djangoplotlydash.com
安装
首先,安装软件包。如果尚未安装Plotly和一些Dash软件包,也将安装它们。
pip install django_plotly_dash
然后,将django_plotly_dash
添加到django settings.py
文件中的INSTALLED_APPS
。
INSTALLED_APPS = [
...
'django_plotly_dash.apps.DjangoPlotlyDashConfig',
...
]
应用程序的路由需要在路由结构中通过适当的include
语句进行注册
一个urls.py
文件:
urlpatterns = [
...
path('django_plotly_dash/', include('django_plotly_dash.urls')),
]
URL中的名称不重要,可以更改。
对于最后的安装步骤,需要迁移来更新 数据库:
./manage.py migrate
注意,由于使用path
函数注册路由,此包需要django版本2.0或更高版本。
进一步配置,包括实时更新以共享应用程序 状态,在online documentation中描述。
演示
源存储库包含一个演示应用程序。克隆repo并启动演示:
git clone https://github.com/GibbsConsulting/django-plotly-dash.git cd django-plotly-dash ./make_env # sets up a virtual environment for development# with direct use of the source code for the package ./prepare_redis # downloads a redis docker container# and launches it with default settings# *THIS STEP IS OPTIONAL* ./prepare_demo # prepares and launches the demo# using the Django debug server at http://localhost:8000
用法
使用现有的破折号应用程序,首先要使用。这个 替换dash
包的Dash
类。举一个非常简单的例子,灵感来自优秀的getting started文档:
importdashimportdash_core_componentsasdccimportdash_html_componentsashtmlfromdjango_plotly_dashimportDjangoDashapp=DjangoDash('SimpleExample')app.layout=html.Div([dcc.RadioItems(id='dropdown-color',options=[{'label':c,'value':c.lower()}forcin['Red','Green','Blue']],value='red'),html.Div(id='output-color'),dcc.RadioItems(id='dropdown-size',options=[{'label':i,'value':j}fori,jin[('L','large'),('M','medium'),('S','small')]],value='medium'),html.Div(id='output-size')])@app.callback(dash.dependencies.Output('output-color','children'),[dash.dependencies.Input('dropdown-color','value')])defcallback_color(dropdown_value):return"The selected color is %s."%dropdown_value@app.callback(dash.dependencies.Output('output-size','children'),[dash.dependencies.Input('dropdown-color','value'),dash.dependencies.Input('dropdown-size','value')])defcallback_size(dropdown_color,dropdown_size):return"The chosen T-shirt is a %s%s one."%(dropdown_size,dropdown_color)
注意,DjangoDash
构造函数需要指定一个名称。然后使用此名称标识中的Dash应用程序
模板:
{% load plotly_dash %} {% plotly_app name="SimpleExample" %}
注册码必须在某个位置
将在任何模型或模板标记尝试使用django进程之前导入该进程。django应用程序示例
在demo子目录中,通过在主urls.py
文件中导入实现这一点;任何views.py
也就足够了。
虽然这个示例允许直接使用现有的^ {CD8>}应用程序,但它不提供共享或更新 内部状态。online documentation提供了有关使用这些 以及其他附加功能。
开发
make_env
脚本设置开发环境,并拉入包
在dev_requirements.txt
文件中指定。check_code
脚本也调用测试套件(使用pytest
)
在包和相关演示上调用pylint
。