I want to include plotly-dash within a Django framework. I want to have one app incorporated into one HTML template that will present line graphs read as pandas dataframe from csv based on the slug entered in the url. The csv files share the same name as the slug to make them easy (I thought) to open.
I want to be able to pull the url path, or the slug, and use that to determine which csv file my app opens and uses to present the data for the line plot.
This worked fine with only Dash. I could use the url and pathname in the callback and use the pathname as the name of the csv file and have the app dynamically open the proper csv file based on the url slug.
@app.callback(
Output('indicator-graphic', 'figure'),
[Input('url', 'pathname'),])
def update_graph(pathname):
df=pd.read_csv('path/to/file/'+pathname+'.csv)
The above code worked fine in the Dash test server. When I incorporate this into Django, the url and pathname won’t work anymore.
I have tried everything I can think of to pass the slug or pathname as an Input to the Dash callback. I cannot get anything to work.
Then, I tried to set the dataframe as global variable outside the callbacks and use django’s HttpRequest function. But when I try that, it returns and empty path.
from django.http import HttpRequest
request = HttpRequest
filename = str(request.path)
df = pd.read_csv('path/to/file'+filename+'.csv)
That returns an empty string.
How can I access the proper csv file? I need to get the slug from the Url and use it to open the proper csv file to render the proper graph.
urls.py
urlpatterns = [
path('', views.home, name='home'),
url('^django_plotly_dash/', include('django_plotly_dash.urls')),
re_path(r'^(?P<slug>[\w-]+)/
```, views.coins, name='coins') ]
views.py
def home(request):
return render(request, ‘home/index.html’)
def coins(request, slug):
coin = Coins.objects.get(slug=slug)
return render(request, ‘home/coins.html’, {‘coin’:coin, ‘chart_twitter’: chart_twitter})