Black Lives Matter. Please consider donating to Black Girls Code today.

Not able to get sample Dash file working with Twisted Web server

Hi, I have been struggling to get this sample dash file working with Twisted Web server. I am using Conda and created a new environment using a Windows machine.

This is the command line that I am using for running an application using twisted web.
twistd web --wsgi labdemo.py --port tcp:8080

And below is the civfdemo.py file. What is the proper syntax in the command line and in the python file to get this working? Presently, the error message the I get is as follows: No such WSGI application: 'labdemo.py’

import dash
import dash_core_components as dcc
import dash_html_components as html

app = dash.Dash()
server = app.server

text_style = dict(color=’#444’, fontFamily=‘sans-serif’, fontWeight=300)
plotly_fig = [dict(x=[1,2,3], y=[2,4,8])]

app.layout = html.Div(children=[
html.H1(children=‘Chart1’),

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

html.P('Enter a Plotly trace type into the text box,' \
       'such as histogram, bar, or scatter.', style=text_style),

    dcc.Graph(id='plot1', 
              figure = {          
                      'data' : plotly_fig , 'layout' : {
                    'title' : 'Test Progress'
                }

            }
        )

])
if name == ‘main’:
app.server.run()

Solution:

I spend another long day and I finally got it to work with a help of a colleague. Being new to Python and of course Dash, my confidence has grown somewhat in programming after this. I found a simpler package called Flask-Twisted, which integrates Flask and Twisted together. Then I had to dig up an old question from the project site as one of the import lines were deprecated. Then sorting through many examples, I was finally able to display plots/figures using dash on a website. So I hope this may help others who may encounter similar issues. Far simpler to use than trying to use mod_wsgi with Apache or using Flask separately with Twisted … at least for someone new to programming.

import flask
from flask_twisted import Twisted

from dash import Dash
import dash_core_components as dcc
import dash_html_components as html

server = flask.Flask(name)
app = Dash(name, server = server)

… Dash code related to plots and figures

if name == ‘main’:
twisted = Twisted(server)
twisted.run(host=‘0.0.0.0’,port=8050, debug=False)