问题
I'm trying to deploy a python dash app to my apache server. I followed the scant amount of information about this configuration that I could find (officials docs; this troubleshooting thread was a bit better). When I visit the website, the page returns a 500 Internal Server Error
, which is described as "Dash object not callable"
in the server error log. These are the config files:
>> cat /var/www/html/wsgi/dashGAF.wsgi
#!/usr/bin/python
import sys
sys.path.insert(0,"/home/ubuntu/dashboards/")
from dashGAF import app as application
>> cat /etc/apache2/sites-available/dash.conf
WSGIDaemonProcess dashGAF user=ubuntu group=ubuntu home=/home/ubuntu threads=5
WSGIScriptAlias /dashGAF /var/www/html/wsgi/dashGAF.wsgi
<Directory /home/ubuntu/dashboards>
WSGIProcessGroup dashGAF
WSGIApplicationGroup %{GLOBAL}
WSGIScriptReloading On
Require all granted
</Directory>
>> cat dashGAF.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, routes_pathname_prefix='/dashGAF/')
server = app.server
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='0.0.0.0')
When I visit my dash app at the_ip_address/dashGAF
, I get a 500 Internal Server Error
. Inspecting the error.log I see:
[Sat Jun 20 04:42:59.502528 2020] [wsgi:error] [pid 6064:tid 140622992238336] [client 118.210.193.245:50042] mod_wsgi (pid=6064): Exception occurred processing WSGI script '/var/www/html/wsgi/dashGAF.wsgi'.
[Sat Jun 20 04:42:59.502675 2020] [wsgi:error] [pid 6064:tid 140622992238336] [client 118.210.193.245:50042] TypeError: 'Dash' object is not callable
Any help in fixing this issue would be GREATLY appreciated! Also any suggestions for changes to the configuration files would be helpful.
Some more details:
- I was getting a module import error, such as the below in my /var/log/apache2/error.log:
[Sat Jun 20 03:38:58.556219 2020] [wsgi:error] [pid 583:tid 140297735726848] [client 118.210.193.245:55574] File "/home/ubuntu/dashboards/dashGAF.py", line 2, in <module> [Sat Jun 20 03:38:58.556265 2020] [wsgi:error] [pid 583:tid 140297735726848] [client 118.210.193.245:55574] import dash [Sat Jun 20 03:38:58.556285 2020] [wsgi:error] [pid 583:tid 140297735726848] [client 118.210.193.245:55574] ImportError: No module named dash
Which I was able to fix by doing a sudo pip install dash==1.13.2
.
I have made all *.py and *.wsgi files
-rwxr-xr-x
I enabled the site config with
sudo a2ensite dash.conf
and reloaded config withudo systemctl reload apache2
.I believe the version of python being run is python2.7 (based on the apache error.log); Not sure exactly how to specify 2.7 or 3.
If I edit the dashGAF.wsgi to have
from dashGAF import server as application
I get a 500 Internal error, but with the following details in the server log:
[Sun Jun 21 06:33:28.181450 2020] [wsgi:error] [pid 12237:tid 139670549669632] [client 118.210.193.245:52221] mod_wsgi (pid=12237): Target WSGI script '/var/www/html/wsgi/dashGAF.wsgi' cannot be loaded as Python module.
[Sun Jun 21 06:33:28.181512 2020] [wsgi:error] [pid 12237:tid 139670549669632] [client 118.210.193.245:52221] mod_wsgi (pid=12237): Exception occurred processing WSGI script '/var/www/html/wsgi/dashGAF.wsgi'.
[Sun Jun 21 06:33:28.181545 2020] [wsgi:error] [pid 12237:tid 139670549669632] [client 118.210.193.245:52221] Traceback (most recent call last):
[Sun Jun 21 06:33:28.181577 2020] [wsgi:error] [pid 12237:tid 139670549669632] [client 118.210.193.245:52221] File "/var/www/html/wsgi/dashGAF.wsgi", line 4, in <module>
[Sun Jun 21 06:33:28.181685 2020] [wsgi:error] [pid 12237:tid 139670549669632] [client 118.210.193.245:52221] from dashGAF import server as application
[Sun Jun 21 06:33:28.181714 2020] [wsgi:error] [pid 12237:tid 139670549669632] [client 118.210.193.245:52221] ImportError: cannot import name server
Possibly there is a useful detail in the bit where it says "Target WSGI script '/var/www/html/wsgi/dashGAF.wsgi' cannot be loaded as Python module." ??
- If I edit the dashGAF.wsgi to have
application = app.server
I get a 404 Not Foun:
from dashGAF import app
application = app.server
回答1:
Typically, you would target the Flask server rather than the Dash app in the wsgi script. That is, instead of
from dashGAF import app as application
it should be
from dashGAF import server as application
回答2:
I've hesitated a little bit on whether I should answer my own question. @emher 's answer was part of the problem - but not the whole solution. I needed to fix a few problems, and much of that troubleshooting was guided by @GrahamDumpleton on github. I'm more than happy for him to provide the answer if he wants.
Nevertheless, here are the problems and fixes that needed to occur:
problems and fixes:
- Target the Flask server as @emher suggested with
from dashGAF import server as application
- it is not necessary to include a
routes_pathname_prefix
, which was resolving the dashboard to https://ip.address/dashGAF/dashGAF` - the
/etc/apache2/sites-available/dash.conf
could be significantly shortened` (see below) - There was a failed request for
_dash-component-suites/dash_renderer/dash_renderer.dev.js
, and I had to addrequests_pathname_prefix='/dashGAF/'
option to myapp = dash.Dash
line (see link on github)
final setup:
/etc/apache2/sites-available/dash.conf
WSGIDaemonProcess dashGAF user=ubuntu group=ubuntu home=/home/ubuntu threads=5
WSGIScriptAlias /dashGAF /var/www/html/wsgi/dashGAF.wsgi
WSGIProcessGroup dashGAF
WSGIApplicationGroup %{GLOBAL}
/var/www/html/wsgi/dashGAF.wsgi
#!/usr/bin/python
import sys
sys.path.insert(0,"/home/ubuntu/dashboards/")
from dashGAF import server as application
dashboards/dashGAF.py As above, but include:
app = dash.Dash(__name__, external_stylesheets=external_stylesheets, requests_pathname_prefix='/dashGAF/')
server = app.server
来源:https://stackoverflow.com/questions/62481788/dash-deployed-on-apache-server-failing-with-dash-object-not-callable