Closed edwardchalstrey1 closed 5 months ago
Updated on the cache-everything
branch, but doesn't seem to make page loading any faster
Certainly! Here are the fully updated versions of your Django view and template code with the changes to implement asynchronous loading of shapes using JavaScript and AJAX.
Django View (views.py):
from django.http import JsonResponse
from django.shortcuts import render
from django.db import connection
from django.contrib.gis.geos import GEOSGeometry
from .models import VideoShapefile
def get_provinces(selected_base_map_gadm='province', simplification_tolerance=0.01):
# ... existing code
def get_shapes(request):
shapes = VideoShapefile.objects.all()
shapes_data = []
for shape in shapes:
shapes_data.append({
'startYear': shape.start_year,
'endYear': shape.end_year,
'polityStartYear': shape.polity_start_year,
'polityEndYear': shape.polity_end_year,
'geometry': shape.geom.geojson,
'name': shape.name,
'colour': shape.colour,
'seshatId': shape.seshat_id,
})
return JsonResponse({'shapes': shapes_data})
def map_view(request):
# ... existing code
return render(request, 'core/spatial_map.html', content)
Django URLs (urls.py):
from django.urls import path
from .views import map_view, get_provinces, get_shapes
urlpatterns = [
path('spatial_map/', map_view, name='spatial_map'),
path('get_provinces/', get_provinces, name='get_provinces'),
path('get_shapes/', get_shapes, name='get_shapes'),
# ... other paths
]
Updated JavaScript in Template (spatial_map.html):
html
Copy code
{% extends "core/seshat-base.html" %}
{% load leaflet_tags %}
<head>
{% leaflet_js %}
{% leaflet_css %}
</head>
<body>
{% block content %}
<script>
document.addEventListener("DOMContentLoaded", function () {
// Fetch shapes asynchronously
fetch('/get_shapes/')
.then(response => response.json())
.then(data => {
var shapesData = data.shapes;
// Plot polities after fetching shapes
plotPolities();
})
.catch(error => console.error('Error fetching shapes:', error));
});
// ... existing JavaScript code
</script>
{% endblock %}
</body>
These changes incorporate the asynchronous loading of shapes using JavaScript and AJAX. The get_shapes endpoint is added to the Django view to provide the shapes data in JSON format, and the template is updated to fetch shapes asynchronously when the page loads. Make sure to adjust the code based on your specific requirements and additional functionalities.
On m Mac:
Cache shapes ideas
cache-everything
(and below)copilot-speedup
branchIdeas 16th Jan 2024
uvicorn
asgi.py
localStorage.setItem
rather than anything new, as was done for colours.