如何在使用flask

时间:2019-07-10 12:56:37

标签: python flask plotly-dash

我想运行带有单击按钮功能的flask脚本,如果单击该脚本,将运行破折号脚本并在127.1.1.1:8050上打开破折号输出,我正在尝试在本地计算机上执行此操作。

我尝试使用subprocess.call,但是它导致了Winerror 10038 An operation was attempted on something that is not a socket,然后我尝试将破折号脚本包装到一个函数中并在flask中调用它,但是它也不起作用,因为它导致127.1.1.1:8050,但是它显示断页并显示消息'This site can't be reached'

Dask和Flask脚本是

### Dash Script ##

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=[
    html.H1(children='Hello Dash'),

    html.Div(children='''
        Dash: A web application framework for Python.
    '''),

    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,host="127.1.1.1")

### Flask Script with subprocess.call ###

from flask import Flask, request, render_template, json
import random, threading, webbrowser
import subprocess

app=Flask(__name__)

viz_url="http://127.1.1.1:{0}".format(8050)

@app.route('/',methods=["GET","POST"])
def viz():

    return '''
        <html>
            <body>
                <p>Click for output:</p>
                <form method="post" action=".">
                    <p><input type="submit" value="Output" /></p>
                </form>
            </body>
        </html>
    ''',threading.Timer(.75, lambda: webbrowser.open(viz_url) ).start(),subprocess.call(["python","dash_script.py"], shell=True)



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




### Flask script with wrapping dash in a function ###



from flask import Flask, request, render_template, json
import random, threading, webbrowser
from dash_script import dash_viz
import subprocess

app=Flask(__name__)

viz_url="http://127.1.1.1:{0}".format(8050)

@app.route('/',methods=["GET","POST"])
def viz():

    return '''
        <html>
            <body>
                <p>CLick for output:</p>
                <form method="post" action=".">
                    <p><input type="submit" value="Output" /></p>
                </form>
            </body>
        </html>
    ''',threading.Timer(.75, lambda: webbrowser.open(viz_url) ).start(),dash_viz()












0 个答案:

没有答案