0

I'm trying to build a dashboard app using django-plotly-dash. I was able to get the app up and running when pulling the data from a .CSV file but this is slow. To speed things up I imported the data into my backend Postgresql server and created a model in the pages app in my Django project. The problem comes when I attempt to import a specific table from my model into my stateless DjangDash app in a different folder. The error I keep getting is "unable to import pages.models"

Folder Structure:

DjangoProject
 >dash_apps
   >finished_apps
     -appOne.py
 >pages
   -__init__.py
   -admin.py
   -apps.py
   -models.py
   -test.py
 >base
   -__init__.py
   -asgi.py
   -routing.py
   -settings.py
   -urls.py
   -wsgi.py
 >templates
  >pages
   -index.html

 -manage.py
  

The app is named appOne.py and the table or class is in the models.py file and is named general_finance_metrics

So far I have tried the following in my appOne.py file with no luck:

from pages.models import general_finance_metrics 
      (error: unable to import 'pages.models' pylint (import-error))
from DjangoProject.pages.models import general_finance_metrics
      (error: unable to import 'DjangoProject.pages.models' pylint (import-error))
from .models import general_finance_metrics
      (error: attempted relative import beyond top-level package pylint (relative-beyond-top-level))
from base.models import general_finance_metrics
      (error: unable to import 'base.models' pylint (import-error))
from base import models
      (error: unable to import 'base' pylint (import-error))
from pages import models
      (error: unable to import 'pages' pylint (import-error))

I dug through these other questions but wasn't able to find an answer that worked unless I overlooked it but I believe my situation is different from these because the DASH app is not inside of a single app but is inside of the DjangoProject.

Django : Unable to import model from another App

Django +2 ImportError: cannot import model

Can't get Python to import from a different folder

import django models classes into utility module

appOne.py code:

import numpy as np
import pandas as pd
import datetime

import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output
import plotly.graph_objs as go
from django_plotly_dash import DjangoDash

from django.db import models
from pages.models import general_finance_metrics  # error occurs here

df = pd.DataFrame(general_finance_metrics.objects.all().values())

models.py code:

class general_finance_metrics(models.Model):
    columnOne = models.TextField(blank=True, null=True)
    columnTwo = models.TextField(blank=True, null=True)
    columnThree = models.TextField(blank=True, null=True)
    
    def __str__(self):
        return self.columnOne 

admin.py code:

from django.contrib import admin
from .models import general_finance_metrics

urls.py code:

from django.urls import path
from dash_apps.finished_apps import appOne
from . import views

urlpatterns = [
    path('', views.index, name='index'),
]

index.html code:

   <body>
      {% load plotly_dash %}
        <div class="{% plotly_class name='appOne' %} card" style="height: 100%; width: 100%;">
          {% plotly_app name='appOne' ratio=0.95 %}
        </div>
   </body>
Michel
  • 769
  • 4
  • 19
notmark
  • 53
  • 7

2 Answers2

1

You need to make sure that your appOne.py file can be imported by views.py (and any other file that is using it). Older versions of python will require __init__.py files in both the dash_apps and finished_apps folders.

Once appOne.py can be imported, a statement along the lines of from pages.models import general_finance_metrics within it will be sufficient.

delsim
  • 86
  • 1
  • 3
0

Both Dash and Django provide application frameworks. If you want to use one inside the other, then you have to choose which one to embed.

Are you trying to use Dash inside of Django? If so then the django-plotly-dash package lets you use Dash apps through Django templates.

On the other hand, if you're trying to use pieces of Django inside a Dash application - which is what it sounds like from your code snippets - then you're probably going to find that quite difficult, if not impossible.

delsim
  • 86
  • 1
  • 3
  • Hi delsim. Thank you for taking the time to answer. I'm using the django-plotly-dash package inside of django. I was able to get this working. The problem is I'm not able to import data from my Django model into the dash app and am forced to pull it from an .csv files instead. Is pulling data from the Django models into the Dash app what you mean by "using pieces of Django inside a Dash application". The dash app is really slow when it has to reload the csv file every time the page refreshes. I was hoping to pull from my postgres server through the django models. – notmark Jun 22 '20 at 18:42
  • You might want to investigate why you can't use the ORM inside your Dash app - this is one of the main reasons django-plotly-dash exists. In particular, Dash callbacks are invoked from within a Django view, so you should be able to just use statements like `MyModel.objects.filter(things_derived_from_callback_arguments)` – delsim Jun 22 '20 at 22:45
  • (adding as I can't edit my original comment) - how is your appOne.py file being imported by Django? – delsim Jun 23 '20 at 16:02
  • I'm importing it in my urls.py file using the line: `From dash_apps.finished_apps import appOne` Then I'm calling it in the body of the HTML. I've added the files and code to question above. Thanks for looking at it again. – notmark Jun 23 '20 at 21:32
  • Can you import any of your files (eg pages.models - dont use relative imports) in that urls.py file? If not, what is the error? – delsim Jun 24 '20 at 00:15
  • Yes, I'm able to use `from pages.models import general_finance_metrics` in the urls.py file. I'm guessing because `models.py` and `urls.py` are in the same folder. The `dash_apps` folder is outside of the `pages` folder. – notmark Jun 24 '20 at 13:19
  • If you remove the import of `general_finance_metrics` from `appOne.py`, can the file then be imported by `urls.py`? If not, what is the error from the python interpreter? If it can, then what is the error from the python interpreter when you put the `general_finance_metrics` import back in? – delsim Jun 24 '20 at 14:43
  • Importing into urls.py works with or without import in appOne.py I think I may have figured it out but I'll have to do a little more testing though. I added an `__init__.py` file to both the dash_apps folder and the finished_apps folder. Without one or the other I still get the linting errors I listed above. – notmark Jun 25 '20 at 20:32