Skip to content

Commit

Permalink
Setting it up with django-plotly-dash
Browse files Browse the repository at this point in the history
  • Loading branch information
tsmbland committed Jan 26, 2024
1 parent f7c00d2 commit 951cab8
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 1 deletion.
4 changes: 4 additions & 0 deletions djangomain/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@
"management",
"crispy_forms",
"crispy_bootstrap4",
"django_plotly_dash.apps.DjangoPlotlyDashConfig",
]

MIDDLEWARE = [
Expand All @@ -91,6 +92,7 @@
"django.contrib.auth.middleware.AuthenticationMiddleware",
"django.contrib.messages.middleware.MessageMiddleware",
"django.middleware.clickjacking.XFrameOptionsMiddleware",
"django_plotly_dash.middleware.BaseMiddleware",
]

ROOT_URLCONF = "djangomain.urls"
Expand Down Expand Up @@ -246,3 +248,5 @@

CRISPY_ALLOWED_TEMPLATE_PACKS = "bootstrap4"
CRISPY_TEMPLATE_PACK = "bootstrap4"

X_FRAME_OPTIONS = "SAMEORIGIN"
1 change: 1 addition & 0 deletions djangomain/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
name="schema-swagger-ui",
),
re_path(r"^redoc/$", schema_view.with_ui("redoc"), name="schema-redoc"),
path("django_plotly_dash/", include("django_plotly_dash.urls")),
]

urlpatterns += [
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ pytz==2023.3.post1
PyYAML==6.0.1
uritemplate==4.1.1
crispy-bootstrap4==2023.1
django-plotly-dash==2.2.2

## Legacy dependency versions
# asgiref==3.3.4
Expand Down
38 changes: 38 additions & 0 deletions validated/dash_apps/finished_apps/daily_validation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
from datetime import datetime
from decimal import Decimal
import plotly.express as px
from dash import dcc, html
from django_plotly_dash import DjangoDash

from station.models import Station
from variable.models import Variable
from validated.functions import daily_validation

# Create a Dash app
app = DjangoDash("DailyValidation")

# Filters (in final app this will get data from forms)
station: Station = Station.objects.order_by("station_code")[7]
variable: Variable = Variable.objects.order_by("variable_code")[0]
start_time: datetime = datetime.strptime("2023-03-01", "%Y-%m-%d")
end_time: datetime = datetime.strptime("2023-03-31", "%Y-%m-%d")
minimum: Decimal = Decimal(-5)
maximum: Decimal = Decimal(28)

# Load data
data: dict = daily_validation(
station=station,
variable=variable,
start_time=start_time,
end_time=end_time,
minimum=minimum,
maximum=maximum,
)

# Create plot
x = data["series"]["measurement"]["time"]
y = data["series"]["measurement"]["average"]
plot = px.line(x=x, y=y)

# Create layout
app.layout = html.Div([dcc.Graph(figure=plot)])
1 change: 0 additions & 1 deletion validated/static/validated/daily_validation.js
Original file line number Diff line number Diff line change
Expand Up @@ -2133,4 +2133,3 @@ function enable_new(){
Array.prototype.unique=function(a){
return function(){return this.filter(a)}}(function(a,b,c){return c.indexOf(a,b+1)<0
});

3 changes: 3 additions & 0 deletions validated/templates/daily_validation_dash.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{% load plotly_dash %}

{% plotly_app name="DailyValidation" %}
6 changes: 6 additions & 0 deletions validated/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

from django.urls import path
from rest_framework.urlpatterns import format_suffix_patterns
from .dash_apps.finished_apps import daily_validation

from . import views

Expand Down Expand Up @@ -67,6 +68,11 @@
views.view_launch_report_calculations,
name="launch_report_calculations",
),
path(
"daily_validation_dash/",
views.DailyValidationDash.as_view(),
name="daily_validation_dash",
),
]

urlpatterns = format_suffix_patterns(urlpatterns)
6 changes: 6 additions & 0 deletions validated/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from django.core.serializers.json import DjangoJSONEncoder
from django.http import HttpResponse, JsonResponse
from django.shortcuts import render
from django.views import View
from django.views.generic import FormView, ListView
from rest_framework import generics

Expand Down Expand Up @@ -426,6 +427,11 @@ def post(self, request, *args, **kwargs):
return render(request, "home/message.html", {"message": form})


class DailyValidationDash(View):
def get(self, request, *args, **kwargs):
return render(request, "daily_validation_dash.html")


def view_launch_report_calculations(request):
"""
Function that receives the signal for calculating reports: hourly, daily, monthly from user interface
Expand Down

0 comments on commit 951cab8

Please sign in to comment.