python 示例django-plotly-dash views.py

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python 示例django-plotly-dash views.py相关的知识,希望对你有一定的参考价值。

from django.shortcuts import render

import dash
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output

import pandas as pd
import plotly.graph_objs as go

from django_plotly_dash import DjangoDash


## Sales tracker

def sales_tracker(request):
  
    app = DjangoDash('SimpleExample')      ## Initialize the app using dpd

    df = pd.read_csv(
        'https://raw.githubusercontent.com/plotly/'
        'datasets/master/gapminderDataFiveYear.csv')

    app.layout = html.Div([
        dcc.Graph(id='graph-with-slider'),
        dcc.Slider(
            id='year-slider',
            min=df['year'].min(),
            max=df['year'].max(),
            value=df['year'].min(),
            marks={str(year): str(year) for year in df['year'].unique()}
        )
    ])

    @app.callback(
        Output('graph-with-slider', 'figure'),
        [Input('year-slider', 'value')])
    def update_figure(selected_year):
        filtered_df = df[df.year == selected_year]
        traces = []
        for i in filtered_df.continent.unique():
            df_by_continent = filtered_df[filtered_df['continent'] == i]
            traces.append(go.Scatter(
                x=df_by_continent['gdpPercap'],
                y=df_by_continent['lifeExp'],
                text=df_by_continent['country'],
                mode='markers',
                opacity=0.7,
                marker={
                    'size': 15,
                    'line': {'width': 0.5, 'color': 'white'}
                },
                name=i
            ))

        return {
            'data': traces,
            'layout': go.Layout(
                xaxis={'type': 'log', 'title': 'GDP Per Capita'},
                yaxis={'title': 'Life Expectancy', 'range': [20, 90]},
                margin={'l': 40, 'b': 40, 't': 10, 'r': 10},
                legend={'x': 0, 'y': 1},
                hovermode='closest'
            )
        }


    context = {}
    return render(request, 'plotly_dash.html')

以上是关于python 示例django-plotly-dash views.py的主要内容,如果未能解决你的问题,请参考以下文章

python实战应用讲解-numpy专题篇实用小技巧(附python示例代码)

python图形GUI基本示例

Joe 的 Erlang websocket 示例的 Python 示例

2018-06-20 中文代码示例视频演示Python入门教程第三章 简介Python

python错误示例

关于一些python爬虫示例代码