Deploy Django-plotly-dash on PythonAnywhere

Hi, Im having some trouble deploying my app to the cloud. Code works locally however when clicking on link to open the dash app within the website it fails. The error log gives a 500 error. I have tried some of the WSGI options in other topics but none have been able to work. I am assuming its to do with the WSGI file settings.

The code is available here GitHub - b-stead/demo

I have three dash apps (dash1, dash2, dash3) which are within the following structure
visuals - dash_apps - finished_apps

current WSGI settings on pythons anywhere are

  # +++++++++++ DJANGO +++++++++++

  import os
  import sys

  path = '/home/TheBoatShed/demo'
  if path not in sys.path:
      sys.path.append(path)
  #
  os.environ['DJANGO_SETTINGS_MODULE'] = 'demosite.settings'
  #
  
  from django.core.wsgi import get_wsgi_application
  application = get_wsgi_application()

 #have tried 
  #from dash1 import dash1 as app
 #application = app.server

Any help to finish this off would be great
Thanks in advance

Hello @brendon,

I havent ran multiple app’s, but I would:

  • Make sure they are running on different ports
  • Have the load balancer send the requests to the proper backend server

Is there any documentation you are trying to follow, or you are just trying to get this going?

Hi @jinnyzor, the apps run well locally, it is in the deployment that I’m battling with.

I subsequently tried to launch via heroku which provided some more insight. The csv files were not being imported. I have resolved that issue and now have a new one, naturally.

The error log provides
File “/home/TheBoatShed/.virtualenvs/myenv/lib/python3.9/site-packages/flask/app.py”, line 792, in debug
return self.config[“DEBUG”]
KeyError: 'DEBUG

Since “DEBUG” is set in the settings.py file I don’t know how to set the variable for the flask app separately.

I have tried to add this manually in the files but has had no effect. the flask documentation suggests this is likely anyway so not surprised.

@delsim do you have any ideas as to how I can resolve this issue?

Hello @brendon,

You should be able to set the config for server[‘DEBUG’] by exposing the flask server in your app. Or just use app.server[‘DEBUG’] and set it to something.

Its strange that you are encountering another issue.

Thanks @jinnyzor

I’ve tried the following options which haven’t worked. Did you have something else in mind in your suggestion above?

In my dash apps

dash1.py

import dash_bootstrap_components as dbc
from django_plotly_dash import DjangoDash
import pandas as pd
import plotly.express as px
from dash import dcc, html
import dash
from dash import Input, Output
from dash_bootstrap_templates import load_figure_template
load_figure_template("superhero")
from pathlib import Path
THIS_FOLDER = Path(__file__).parent.resolve()

app = DjangoDash('demo1',external_stylesheets=[dbc.themes.SUPERHERO])
#test for deployment
app.config['DEBUG']=False

DjangoDash doesn’t have a config option so this failed

I then tried

from flask import Flask
app1 = Flask(__name__)
app1.config['DEBUG']=False

This brings back the key error for DEBUG

Using

app = DjangoDash('demo1',external_stylesheets=[dbc.themes.SUPERHERO])
app.server[‘DEBUG’]=False

Or

from flask import Flask

app1 = Flask(__name__)

app1.server[‘DEBUG']=False

in the dash1.py blows up whole site

I then tried to import DEBUG as per this example How to set environment variables for your web apps (for SECRET_KEY etc) | PythonAnywhere help from the .env file into the WSGI file, this overrode DEBUG in settings.py to True for the whole site

Did you have something else in mind?

If you run just one, does it work?


Also, wanted to check with you and see if on your admin side of things it shows these dash apps in stateless apps or under dash apps? This is what my Django admin portal looks like in relation to dash. 516 stateless apps and 0 dash apps as I’m hosting dash on its own server.

1 Like

@brendon how are you accessing your apps? Are you actually using Django or just through something like flask?

I have the dash apps registered as stateless in the admin page.

Thank you for taking the time to respond.

I have another webapp that runs Dash pages successfully. It pulls data from csv files and displays the graphs I need. I do like the simplicity of that solution in general and could use it in this instance within the demo site.

I am looking to build a website that supports different users (coaches & athletes) accessing different levels of data. Hence why I am trying to integrate Django & Dash through Django-Plotly-Dash.

Thank you for the video suggestion I have watched that previously and where most of my specific code for the Django-dash elements have come from. I will go through it again to check if I have gone off somewhere.

From your comment above do you see my issue lying with Django or potentially with the setup in python anywhere? And will your recommended setup of separate servers work if I then need to pull data from the database?

Hello @brendon,

I use flask-login with a database of users and perms at work.

I don’t know where the difference lies with the ease of user with Django, but know that it is possible to do without. :stuck_out_tongue_winking_eye:

@brendon this could just be incompatible versions of python packages. Are you able to see what versions are in use? If you are not explicitly fixing package versions it is not uncommon to find inconsistencies when publishing to a second platform.

1 Like

Thanks. There were a couple of packages that were fixed to manage some compatibility issues. Updated Django-Plotly-Dash and its up and running.

Thanks

1 Like