通过 Flask 框架将 Dash Barchart 插入 HTML 页面 [英] Insert Dash Barchart into a HTML page via Flask Framework

查看:109
本文介绍了通过 Flask 框架将 Dash Barchart 插入 HTML 页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在我的网站中插入一个用 Flask 编写的条形图.这是我的 app.py 的样子:

from flask import Flask,render_templateapp = Flask(__name__)@app.route("/")定义索引():return render_template('index.html', title="主页")如果 __name__=='__main__':应用程序运行(调试=真)

这是我的条形图的样子(chart.py):

# -*- 编码:utf-8 -*-导入破折号将 dash_core_components 导入为 dcc将 dash_html_components 导入为 htmlexternal_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css']app = dash.Dash(__name__, external_stylesheets=external_stylesheets)app.layout = html.Div(children=[dcc.Graph(id='示例图',数字={'数据': [{'x': [1, 2, 3], 'y': [4, 1, 2], 'type': 'bar', 'name': 'SF'},{'x': [1, 2, 3], 'y': [2, 4, 5], 'type': 'bar', 'name': u'Montréal'},],})])如果 __name__ == '__main__':app.run_server(调试=真)

这是我的 html 的样子 (index.html):

<html lang="zh-cn"><title>W3.CSS 模板</title><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1"><link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css"><link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Lato"><link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Montserrat"><link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css"><风格>body,h1,h2,h3,h4,h5,h6 {font-family: "Lato", sans-serif}.w3-bar,h1,button {font-family: "Montserrat", sans-serif}.fa-anchor,.fa-coffee {font-size:200px}</风格><身体><!-- 导航栏--><div class="w3-top"><div class="w3-bar w3-red w3-card w3-left-align w3-large"><a class="w3-bar-item w3-button w3-hide-medium w3-hide-large w3-right w3-padding-large w3-hover-white w3-large w3-red" href="javascript:void(0);"onclick="myFunction()" title="切换导航菜单"><i class="fa fa-bars"></i></a><a href="#" class="w3-bar-item w3-button w3-padding-large w3-white">首页</a><a href="#" class="w3-bar-item w3-button w3-hide-small w3-padding-large w3-hover-white">链接 1</a><a href="#" class="w3-bar-item w3-button w3-hide-small w3-padding-large w3-hover-white">链接 2</a><a href="#" class="w3-bar-item w3-button w3-hide-small w3-padding-large w3-hover-white">链接 3</a><a href="#" class="w3-bar-item w3-button w3-hide-small w3-padding-large w3-hover-white">链接 4</a>

<!-- 小屏幕上的导航栏--><div id="navDemo" class="w3-bar-block w3-white w3-hide w3-hide-large w3-hide-medium w3-large"><a href="#" class="w3-bar-item w3-button w3-padding-large">链接 1</a><a href="#" class="w3-bar-item w3-button w3-padding-large">链接 2</a><a href="#" class="w3-bar-item w3-button w3-padding-large">链接 3</a><a href="#" class="w3-bar-item w3-button w3-padding-large">链接 4</a>

<!-- 标题--><header class="w3-container w3-red w3-center" style="padding:128px 16px"><h1 class="w3-margin w3-jumbo">开始页面</h1><p class="w3-xlarge">模板来自 w3.css</p><button class="w3-button w3-black w3-padding-large w3-large w3-margin-top">开始使用</button></标题><!-- 第一个网格 --><div class="w3-row-padding w3-padding-64 w3-container"><div class="w3-content"><div class="w3-twothird"><h1>条形图</h1><h2>在此处插入条形图</h2>

</html>

我想要一个网站,我可以在其中嵌入 dash 或 seaborn 或 plotly 或可能的 d3 图表到一个页面上.使用虚线图,我希望制作一个看起来像这样的网站:

我该怎么做?

编辑 1 05/04/19.这是我的文件结构:

解决方案

import dash将 dash_core_components 导入为 dcc将 dash_html_components 导入为 html从烧瓶导入烧瓶,render_templateserver = Flask(__name__)@server.route('/名称')定义名称():返回 render_template('index.html')external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css']布局 = """<!DOCTYPE html><html lang="zh-cn"><title>{%title%}</title>{%图标%}{%metas%}<meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1"><link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css"><link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Lato"><link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Montserrat"><link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">{%css%}<风格>body,h1,h2,h3,h4,h5,h6 {font-family: "Lato", sans-serif}.w3-bar,h1,button {font-family: "Montserrat", sans-serif}.fa-anchor,.fa-coffee {font-size:200px}</风格><身体><!-- 导航栏--><div class="w3-top"><div class="w3-bar w3-red w3-card w3-left-align w3-large"><a class="w3-bar-item w3-button w3-hide-medium w3-hide-large w3-right w3-padding-large w3-hover-white w3-large w3-red" href="javascript:void(0);"onclick="myFunction()" title="切换导航菜单"><i class="fa fa-bars"></i></a><a href="#" class="w3-bar-item w3-button w3-padding-large w3-white">首页</a><a href="#" class="w3-bar-item w3-button w3-hide-small w3-padding-large w3-hover-white">链接 1</a><a href="#" class="w3-bar-item w3-button w3-hide-small w3-padding-large w3-hover-white">链接 2</a><a href="#" class="w3-bar-item w3-button w3-hide-small w3-padding-large w3-hover-white">链接 3</a><a href="#" class="w3-bar-item w3-button w3-hide-small w3-padding-large w3-hover-white">链接 4</a>

<!-- 小屏幕上的导航栏--><div id="navDemo" class="w3-bar-block w3-white w3-hide w3-hide-large w3-hide-medium w3-large"><a href="#" class="w3-bar-item w3-button w3-padding-large">链接 1</a><a href="#" class="w3-bar-item w3-button w3-padding-large">链接 2</a><a href="#" class="w3-bar-item w3-button w3-padding-large">链接 3</a><a href="#" class="w3-bar-item w3-button w3-padding-large">链接 4</a>

<!-- 标题--><header class="w3-container w3-red w3-center" style="padding:128px 16px"><h1 class="w3-margin w3-jumbo">开始页面</h1><p class="w3-xlarge">模板来自 w3.css</p><button class="w3-button w3-black w3-padding-large w3-large w3-margin-top">开始使用</button></标题><!-- 第一个网格--><div class="w3-row-padding w3-padding-64 w3-container"><div class="w3-content"><div class="w3-twothird"><h1>条形图</h1><h2>在此处插入条形图</h2>{%app_entry%}

{%config%}{%scripts%}{%renderer%}</html>"""app = dash.Dash(__name__, external_stylesheets=external_stylesheets, server=server, index_string=layout)app.layout = html.Div(children=[dcc.Graph(id='示例图',数字={'数据': [{'x': [1, 2, 3], 'y': [4, 1, 2], 'type': 'bar', 'name': 'SF'},{'x': [1, 2, 3], 'y': [2, 4, 5], 'type': 'bar', 'name': u'Montréal'},],'布局': {'title': 'Dash 数据可视化'}})])如果 __name__ == '__main__':app.run_server(调试=真)

I'm trying to insert a barchart into my website which has been written in Flask. Here is what my app.py looks like:

from flask import Flask, render_template
app = Flask(__name__)

@app.route("/")
def index():
    return render_template('index.html', title="Mainpage")

if __name__=='__main__':
    app.run(debug=True)

Here is what my barchart looks like (chart.py):

# -*- coding: utf-8 -*-
import dash
import dash_core_components as dcc
import dash_html_components as html

external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css']

app = dash.Dash(__name__, external_stylesheets=external_stylesheets)

app.layout = html.Div(children=[

    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': u'Montréal'},
            ],
        }
    )
])

if __name__ == '__main__':
    app.run_server(debug=True)

And here is what my html looks like (index.html):

<!DOCTYPE html>
<html lang="en">
<title>W3.CSS Template</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css">
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Lato">
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Montserrat">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<style>
body,h1,h2,h3,h4,h5,h6 {font-family: "Lato", sans-serif}
.w3-bar,h1,button {font-family: "Montserrat", sans-serif}
.fa-anchor,.fa-coffee {font-size:200px}
</style>
<body>

<!-- Navbar -->
<div class="w3-top">
  <div class="w3-bar w3-red w3-card w3-left-align w3-large">
    <a class="w3-bar-item w3-button w3-hide-medium w3-hide-large w3-right w3-padding-large w3-hover-white w3-large w3-red" href="javascript:void(0);" onclick="myFunction()" title="Toggle Navigation Menu"><i class="fa fa-bars"></i></a>
    <a href="#" class="w3-bar-item w3-button w3-padding-large w3-white">Home</a>
    <a href="#" class="w3-bar-item w3-button w3-hide-small w3-padding-large w3-hover-white">Link 1</a>
    <a href="#" class="w3-bar-item w3-button w3-hide-small w3-padding-large w3-hover-white">Link 2</a>
    <a href="#" class="w3-bar-item w3-button w3-hide-small w3-padding-large w3-hover-white">Link 3</a>
    <a href="#" class="w3-bar-item w3-button w3-hide-small w3-padding-large w3-hover-white">Link 4</a>
  </div>

  <!-- Navbar on small screens -->
  <div id="navDemo" class="w3-bar-block w3-white w3-hide w3-hide-large w3-hide-medium w3-large">
    <a href="#" class="w3-bar-item w3-button w3-padding-large">Link 1</a>
    <a href="#" class="w3-bar-item w3-button w3-padding-large">Link 2</a>
    <a href="#" class="w3-bar-item w3-button w3-padding-large">Link 3</a>
    <a href="#" class="w3-bar-item w3-button w3-padding-large">Link 4</a>
  </div>
</div>

<!-- Header -->
<header class="w3-container w3-red w3-center" style="padding:128px 16px">
  <h1 class="w3-margin w3-jumbo">START PAGE</h1>
  <p class="w3-xlarge">Template by w3.css</p>
  <button class="w3-button w3-black w3-padding-large w3-large w3-margin-top">Get Started</button>
</header>

<!-- First Grid -->
<div class="w3-row-padding w3-padding-64 w3-container">
  <div class="w3-content">
    <div class="w3-twothird">
      <h1>Bar Chart</h1>

        <h2>Insert bar chart here</h2>

  </div>
</div>

</body>
</html>

I want a site where I can embed dash or seaborn or plotly or possibly d3 charts onto one page. With the dash chart, I'm hoping to make a site that looks something like this:

How would I go about doing this?

EDIT 1 05/04/19. Here is what my file structure looks like:

解决方案

import dash
import dash_core_components as dcc
import dash_html_components as html

from flask import Flask, render_template

server = Flask(__name__)

@server.route('/name')
def name():
    return render_template('index.html')

external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css']

layout = """
<!DOCTYPE html>
<html lang="en">
<title>{%title%}</title>
{%favicon%}
{%metas%}
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css">
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Lato">
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Montserrat">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
{%css%}
<style>
body,h1,h2,h3,h4,h5,h6 {font-family: "Lato", sans-serif}
.w3-bar,h1,button {font-family: "Montserrat", sans-serif}
.fa-anchor,.fa-coffee {font-size:200px}
</style>
<body>

<!-- Navbar -->
<div class="w3-top">
  <div class="w3-bar w3-red w3-card w3-left-align w3-large">
    <a class="w3-bar-item w3-button w3-hide-medium w3-hide-large w3-right w3-padding-large w3-hover-white w3-large w3-red" href="javascript:void(0);" onclick="myFunction()" title="Toggle Navigation Menu"><i class="fa fa-bars"></i></a>
    <a href="#" class="w3-bar-item w3-button w3-padding-large w3-white">Home</a>
    <a href="#" class="w3-bar-item w3-button w3-hide-small w3-padding-large w3-hover-white">Link 1</a>
    <a href="#" class="w3-bar-item w3-button w3-hide-small w3-padding-large w3-hover-white">Link 2</a>
    <a href="#" class="w3-bar-item w3-button w3-hide-small w3-padding-large w3-hover-white">Link 3</a>
    <a href="#" class="w3-bar-item w3-button w3-hide-small w3-padding-large w3-hover-white">Link 4</a>
  </div>

  <!-- Navbar on small screens -->
  <div id="navDemo" class="w3-bar-block w3-white w3-hide w3-hide-large w3-hide-medium w3-large">
    <a href="#" class="w3-bar-item w3-button w3-padding-large">Link 1</a>
    <a href="#" class="w3-bar-item w3-button w3-padding-large">Link 2</a>
    <a href="#" class="w3-bar-item w3-button w3-padding-large">Link 3</a>
    <a href="#" class="w3-bar-item w3-button w3-padding-large">Link 4</a>
  </div>
</div>

<!-- Header -->
<header class="w3-container w3-red w3-center" style="padding:128px 16px">
  <h1 class="w3-margin w3-jumbo">START PAGE</h1>
  <p class="w3-xlarge">Template by w3.css</p>
  <button class="w3-button w3-black w3-padding-large w3-large w3-margin-top">Get Started</button>
</header>

<!-- First Grid -->
<div class="w3-row-padding w3-padding-64 w3-container">
  <div class="w3-content">
    <div class="w3-twothird">
      <h1>Bar Chart</h1>

        <h2>Insert bar chart here</h2>
        {%app_entry%}
  </div>
</div>
  {%config%}
  {%scripts%}
  {%renderer%}
</body>
</html>
"""

app = dash.Dash(__name__, external_stylesheets=external_stylesheets, server=server, index_string=layout)

app.layout = html.Div(children=[
    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': u'Montréal'},
            ],
            'layout': {
                'title': 'Dash Data Visualization'
            }
        }
    )
])

if __name__ == '__main__':
    app.run_server(debug=True)

这篇关于通过 Flask 框架将 Dash Barchart 插入 HTML 页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
Python最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆