GrahamDumpleton / mod_wsgi

Source code for Apache/mod_wsgi.
Apache License 2.0
1.02k stars 268 forks source link

Dash does not work with mod_wsgi, python3.9 and win... #709

Closed user4837 closed 2 years ago

user4837 commented 3 years ago

Hi, I followed all docs to install mod_wsgi correctly under win. Happily the Flask python hello world program runs smoothly! however, instead of flask, I need to use dash. I use a demo dash program from their website, also runs smoothly in VSCODE with the development webserver under localhost:8050. So far so good. I've tried to configure everything correctly now for use dash with WSGI on Apache 2.4. Apache starts smoothly, no error messages at all. However, when calling the website through apache, it starts with the "loading...." from dash, but then nothing follows. looking into the chrome console, I can see many errors like ERR_NAME_NOT_RESOLVED for many dash dependencies. Thus, it seems that when running on apache the dash dependencies are not found. I've tried all workaround I could find from 10... of website, all dont work for me so for.... hope someone has a good idea here.... a bit frustrating... please find below the code segments... THANKS!!!!!!!!!

the app3.py is in directory: e:/quantconnect_lean21/data/Cryptohopper_API_apps/app_test_flask/

the app3.py:

=============================================================
`# -*- coding: utf-8 -*-

# Run this app with `python app.py` and
# visit http://127.0.0.1:8050/ in your web browser.

import dash
import dash_core_components as dcc
import dash_html_components as html
import plotly.express as px
import pandas as pd
from flask import Flask

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

app = dash.Dash(__name__)

server = app.server
app.scripts.config.serve_locally = True
app.css.config.serve_locally = True

# assume you have a "long-form" data frame
# see https://plotly.com/python/px-arguments/ for more options
df = pd.DataFrame({
    "Fruit": ["Apples", "Oranges", "Bananas", "Apples", "Oranges", "Bananas"],
    "Amount": [4, 1, 2, 2, 4, 5],
    "City": ["SF", "SF", "SF", "Montreal", "Montreal", "Montreal"]
})

fig = px.bar(df, x="Fruit", y="Amount", color="City", barmode="group")

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=fig
    )
])

if __name__ == '__main__':

    app.run_server(debug=True)

========================================================== This is the yourapplication.wsgi in the same dir as the py file

import sys
sys.path.insert(0, 'e:/quantconnect_lean21/data/Cryptohopper_API_apps/app_test_flask/')
sys.path.append("e:/ProgrammingSoftware/Python39/Lib/site-packages;e:/quantconnect_lean21/data/Cryptohopper_API_apps/app_test_flask")

from app3 import app
application = server

============================================================ This is the apache vhost file:

<VirtualHost *>
ServerAdmin webmaster@localhost
ServerName localhost
WSGIScriptAlias / "e:/quantconnect_lean21/data/Cryptohopper_API_apps/app_test_flask/yourapplication.wsgi" 
##\
##   process-group=app_test_flask application-group=%{GLOBAL}
DocumentRoot "E:/quantconnect_lean21/data/Cryptohopper_API_apps/app_test_flask"
<Directory "e:/quantconnect_lean21/data/Cryptohopper_API_apps/app_test_flask">
        Order deny,allow
        Allow from all
        Require all granted

</Directory>
AddHandler wsgi-script .wsgi
WSGIScriptReloading On

WSGIApplicationGroup %{GLOBAL}
ErrorLog "e:/quantconnect_lean21/data/Cryptohopper_API_apps/app_test_flask/error.log"
CustomLog "e:/quantconnect_lean21/data/Cryptohopper_API_apps/app_test_flask/access.log" common
</VirtualHost>

=================================================== typical errors from the console:

GET http://_dash-component-suites/dash/deps/polyfill@7.v1_21_0m1629874495.12.1.min.js net::ERR_NAME_NOT_RESOLVED localhost/:21 GET http://_dash-component-suites/dash/deps/react@16.v1_21_0m1629874495.14.0.min.js net::ERR_NAME_NOT_RESOLVED

GrahamDumpleton commented 3 years ago

Your browser is requesting a URL of:

http://_dash-component-suites/dash/deps/polyfill@7.v1_21_0m1629874495.12.1.min.js

which doesn't appear to include a proper host name in it, so the page generated by the web application appears to be the problem.

Do you have any idea where _dash-component-suites is coming from? That is what the browser is thinking is the hostname.

BTW, get rid of:

AddHandler wsgi-script .wsgi
WSGIScriptReloading On

neither is needed.

On Windows if you want to have new code picked up, you need to restart Apache.

user4837 commented 3 years ago

many thanks! I can delete both lines from WSGI, just putted in cos found in some places, but also did not change anything of the problem... Unfortunately, I have no idea at all where these _dash-component-suites are coming from, for sure not configured by me. You have the full source of all files involved, nothing else configured by me. It's running with no problem in VSCode or just by python.

GrahamDumpleton commented 3 years ago

Use Google to search for the quoted term "_dash-component-suites" and you will find pages about it.

user4837 commented 3 years ago

yes, also did those searches, but unfortunately most of those locations are for linux and others don't show really clear solution. Anyway, trying out different ideas from those searches... Just a short question, also related maybe: When a python WSGI web page runs on Apache, the web server probably just start the python app via WSGI. Is is possible to get a feedback of the PID of the application started to ensure that after shutdown of apache (or restart of webpage) also the python app will be shutdown properly and does not continue to run even when a new app instance will be started with a restart of apache!? many thanks

GrahamDumpleton commented 3 years ago

The Python WSGI application runs inside of the Apache process. In other words, the Python interpreter is embedded inside of Apache. So there is no risk that the Python WSGI application isn't shutdown when Apache is shutdown as they are the same process. That they are the same process is why when you make code changes you have to restart Apache to pick up the new version of the code.

user4837 commented 3 years ago

many thanks for explanation again! Just one more thing: does the python/apache process already starts when the python service is started or just after the webpage was called first time from any user? many thanks