Hypermedia driven maps
Transcript
By Speaker 1, Andrea Guerra, Speaker 3. From Django Day Copenhagen 2024. Published October 13, 2024.
Watch the video Markdown version 4,008 words · auto-generated · en
Summary
Andrea Guerra demonstrates a simple Django map built around hypermedia: Django renders HTML fragments and GeoJSON, while HTMX swaps those fragments without reloading the page and Leaflet remains an interactive map island. The example uses Django GeoJSON with a lightweight location model, avoiding PostGIS and heavier GIS dependencies for basic markers. A single persistent map updates its markers when users navigate between list and detail views, and map clicks set latitude and longitude in an edit form. Guerra presents this as a proof of concept for keeping basic map features simple, while noting that tasks such as drawing polygons may justify a library like Django Leaflet.
Transcript
Automatically transcribed, so expect mistakes in names and technical terms. Filler words are removed.
0:00 Introduction and Background
Speaker 1
Much further ado. I see people enjoying the coffee and the tea shouldn't have mentioned that. And the stick.
Andrea Guerra
And the stickers.
Speaker 1
But we should really get started. Five seconds. Mute yourselves. Okay. We're ready. On stage, Andrea, welcome. Please give Andrea a big hand.
Andrea Guerra
Do you hear me? Okay. So we will be talking about maps again, but just in a different kind of way because we've seen that Rosamund worked with React and I will work with hypermedia And I'm Andrea Guerra. Here you have some links of my projects, of my blog and so on. And I have to tell you that I'm not a developer, I'm an architect. So I normally design buildings. I design renovations and things like that. But I've been into programming since I was a teenager.
From a time in which You didn't have floppy disk and you have to load you had to load things with cassette recorders And then I started a blog and 25 years ago. It was a pure HTML. And then it evolved in time, becoming WordPress. So I started learning PHP and then I noticed that Python was better. And so I shifted everything to Django. And now I'm very proud because in the last weeks I released a Python project.
2:17 Hypermedia-Driven Maps
Andrea Guerra
in the PyPy repository, Django GeoCAD, which takes drawings from AutoCAD and brings it into web maps. And so I give my contribution to this okay hypermedia driven maps. What is hypermedia? Here I have some quotes from the author of HTMX Carson Gross and hypermedia was the old way of making websites. You have you had multi page, you had everything Written in HTML tags and every time you had a request-response cycle
you had to reload the page. So that was very annoying. And so then came the single page applications and the new hypermedia adds to normal hypermedia adds the SPA flavor because it doesn't reload the whole page. It loads just small chunks of HTML replacing in targets so to speak. So it looks like an SPA but in fact it's it's the old way the old way of making websites. Another concept that that I borrow from Carson
is that of the island of interaction So inside our hypermedia application we can have some isolated islands of interaction which are normally in JavaScript. They live on the client. But they are in some way managed and controlled by the hypermedia. And of course in our case the this island will be a leaflet a leaflet map like the one that Rosamund showed us before and I use for the swapping trick, I use HTMX, but there are also other
Libraries like Alpine or And poly and so on. I won't dig too much into HTMX because there will be a following talk about this so I will just scratch a little bit the surface. The important thing is that we Zwap ch chunks, but we don't reload the page. This is the goal of everything now is to not reload the page. So this is how our hypermedia driven app will look like. We have in the orange frame we have we have the hypermedia part. the part that will be swapped. And it has the content, it has the user interface, it has the data for the map.
and it also brings state for the island of interaction. On the right you have the island And if of course you I show you an island at Scapri, if you haven't recognized it. And of course, this part is fully interactive. You can pan, you can zoom like you usually do with a map when you have it on a web a website. And here I show you the interactions that happen between all the all the moving parts let's say You see I have those black arrows. The big one goes from the hypermedia to the island.
But sometimes the island will give back something and then you will have the response the request-response cycle from our chunk to the Django application and also the island will communicate with an external server that will download all the tiles that are that you need to make up the map, all the small tiles that make up the map. And you will see that you see I have a yellow a yellow arrow. Sometimes the island of interaction
6:57 Django App and Geographic Data
Andrea Guerra
will send directly to the Django app will send some requests. So we are talking about Django. Of course there is a Django project at the base of everything. And here are the requirements. I use the latest Django and apart from other not relevant libraries I use Django HTMX by Adam Johnson, which is very clever because it tells you when request comes from HTMX. So you can recognize when HTMX is involved and so when you are expecting to swap a part of the of
the page. And then I use a Django GeoJSON, which is kind of a serializer, and it writes all the geographic data into a format with this which is called GeoJSON, which is a JSON in fact. And it's very interesting because it does not rely on postGis or not even Postgres. Not even GDAL, which is the basic library, which is written in C<unk>. Without the that library nothing works. But it's a little bit tricky to have it on your
On your system. So in this way I can have the lightest application possible And I have an app, of course, which has just one model, which is called location and it holds the title, the description of the location, and then the point field which is taken from the GeoJSON package. It's a point field. It writes in GeoJSON format where is located geographically the location. Apart from this, there is another
method pretty important which is called pop up pop-up content, and it returns an HTML string with the absolute URL of the location and the title of the location. And this will be useful because it will show up when we click on a marker and have the pop-up coming out. And okay, we start we run the server Of course, this is the first time that we enter the website and a location list view is called. It's a generic list view
10:01 Templates and Map Initialization
Andrea Guerra
with some little tweaks like example I override the get template names and Here is where Django HTMX come comes handy because if it's if it's not an HTMX request we just get the whole page, the full page But if it's not, you render only the partial, only the part that will be swapped. So this is very important. And of course we are downloading the whole page, so let's see how the base template is structured in the head we will download all the leaflet
The leaflet stuff the so the basically we will download the all the package that lets us work with the with a map And in the body we will include the hypermedia partial, the single part that will be swept. effectively. Then we have another division with the which I call the island and the this is where the map will be will be placed and we have to call a script a custom script that manages the function functionality of the map
and at the end we have to download HTMX because unless nothing works So let's see the hypermedia partial. Obviously it differs if it's a list view, if it's a detail view, update view and so on. But the structure is this. You load some template tags from Django GeoJSON. You will have some content, you will have the user interface, and there is a very important at the end There is this script which has an ID map data and it uses
GeoJSON template tag, the GeoJSO feature, and It gets the objekt lists. This miss that dets all the locations and it transforms it into a geogasson structur. And it holds not only the geographic the geographic information, but also you see the pop-up content. This means that it holds also all the links that we wrote before with the method, with the previous method into the into the model. location model. We have to see a little bit of
JavaScript because it's important. We this is the map script, this is the file that manages the leaflet the leaflet map. And you see at the beginning there is the map is initiated but this is the only time that we will do it just the first time. And then we call a base map with all the tiles, the tiles that form the map and it's important to have a layer group where we will place all the all the markers because we want to be able to erase them
in other moments because we don't have we don't want to touch the map the whole map but just erase some information and add some information. In fact you see that there is this get collections find that removes the objects from the marker layer, then it goes and reads from the hypermedia part, reads all the GeoJSON information. and then it transforms it into the markers that we have on the map. And at the end it fits the bounds of all these markers and it zooms in and so you will see this.
You will see all the markers and of course the markers were generated automatically so they cover up all the surface of the globe. But we can zoom a little bit in and for example we can click on one of these markers and the pop-up will come forward and clicking on Capri we should go to the detail view of our application But of course if I have to be careful because if I leave the If I leave the link like I I've shown you before in the in the pop-up
15:22 HTMX Navigation and Map Refresh
Andrea Guerra
content method, it would happen that all the whole page would be reloaded. And of course we don't want this. So I told you a lie before. It's not It's not written with the hypertext reference with the your URL inside. The hypertext reference is null. And there will be an attribute with an on-click that calls a function, open location, which has an argument with the URL. And in if we get back to the map script, we will see that this function has inside an HTMX Ajax
call. So in this moment we are seeing HTMX from the inside under the hood because normally we don't use it this way HTMX makes an Ajax call, an asynchronous call with a get method to an IR URL. And at the end you see the target of this of this request. So He's saying you get this you get this response and you place it into a piece of the DOM. Okay So we go to the
location detail view. It's a generic detail view. and it has the get template names as the list view that we have seen before but it tweaks also the dispatch method this is very important And this method says if the request is made from HTMX, you have to add this header to the response. And the response adds this header which comes of which is a custom HTMX header which triggers an event after the swap you see HTMX
trigger after the swap that that means that after that after the piece of HTML is swapped you have to trigger that that event and what happens Just there is a in MapScript there is an event listener that refreshes all the data. So it erases back everything that is on the map and reads again into the new Abermedia part of the of the DOM reads the GeoJSON data and puts the new the new markers on the map fits the bounds and zooms So what you will see is this
this: you will see the detail view. So we have zoomed into Capri and now we are near the funicolare the funicolare station. Which is I don't know if you've been there. And you see we have some content and we have some user interface, but we see that We would like the marker to be a little bit displaced over the south because the relevant part here is the piazzetta and not funicolare station. So we want to update this. Okay normally when we use
19:10 Editing Locations on the Map
Andrea Guerra
maps and we want to create or update. Normally we need a widget and normally we will we will load another library like Django Leaflet which has these the build built-in widgets so that we can have map widgets on our front end. But we don't we don't want that because if we do that we will have to reload everything back. We want to use the same map. The same map that we are using for seeing the list view, the detail view. Okay, so we click on update location.
This is a real HTMX request. So we have attributes. We will see that later in another talk. What happens? Okay, we are doing an we are reaching an update view. We of course we have a form. In this form we will have some new fields, latitude and longitude, because it's a little bit tricky to use A JSON inside a form. So I don't call the germ field. You see in the klass mate, I don't call the germ field. I make these two
fields. I can validate them if they are not correct because latitude can't be more than 90 degrees or less than minus 90 and so on. Into the update view we will have a get initial which gets the latitude and longitude initials from the geographic field which is in the database. And when the all the process ends with format valid, we will do exactly the opposite and we will fill the geographic field with the lat and long clean data. So
there is another thing that I add to the location update view into the context data. I put an the map status and we see we will see why it's it it's rendered in the location update it's render HTML in the template it's rendered as a GeoJSON. This JSON script template tag is built in a Django. And so what happens here we are we have the here we have the update view So you see on the on the right we have the form and we can change the title we can change the
the description and We could also manually change latitude and longitude, but of course that would be a little bit a mess. But if we click on the map What happens? Let's say that we move, we don't want the marker to be on the on the red cross, but we want it to be on the piazzetta. So we just click on the piazzetta and the fields will be automatically updated and the marker will be placed where we have clicked. And this using without using special widgets, we are just using the previous map,
which has never changed. in its in its it has never been reiniti initi initiated excuse me. And this You see that on the bottom you have a map on click. Function. This is built in left in Leaflet and it fires an event And what the update lat longer event it does what does it do? It reads the map status on the on the HTML and it see it looks for the map
on click key. And if it's true it goes on because we want this clicking on the map to work only if we want to update or create a new location and not just make it fire when we are looking at detail view or list view. So when if the map status map on click is true, we update the Latin long fields and we delete the objects from the marker layer and we put the next the updated position with using the event
Geographic coordinates. So I finish my talk I hope I I've been you've appreciated it and that's all
25:03 Questions and Discussion
Speaker 1
Thank you so much. Can I ask you what the again what was the project for?
Andrea Guerra
no I I've done other the other project, no I or this one No, this one is just to show how to use a map without fancy stuff, without having too much libraries. Just it's it's just proof of concept. Yeah, that's that's all it's just to show that you can do this without having too much stuff around. Okay. That's all. Because I I've used PostGIS, I've used Giorgiango in other projects when you have you need more complexity. In example If you don't have to just click on a map and put a marker on a map, but
if you have to draw a polygon on a map This couldn't be possible or maybe it could be possible but very complicated. So I would just download Django Leaflet and take his widgets and use them here. But if you do so you need some libraries, other libraries. So it gets it swells in some way.
Speaker 1
Yeah. Thank you so much. There is time for questions. It was really nice to see code. I felt I felt the itch. Yeah, yeah. I don't know the keyboard starting to click out there too. You can't show developers too much
Andrea Guerra
But you can you can see the whole code on in that over there on GitHub over there. So Okay. No questions, but maybe it's better. No we can go we can go onward.
Speaker 1
There is a pop quiz afterwards and it's important with these details, by the way.
Speaker 3
I'll just sneak in one again. I do I'm sort of lost in how do you go from being an architect and like actually architect and buildings, how do you end up doing this?
Andrea Guerra
No look actually in architecture w they are trying to be a little bit more into programming because now there is all this hype of building information management. No, you want to a building you want to be it like a database You know there is a carpet. This carpet is here and it's made from wool and it's the dimensions are this because maybe you have twenty carpets in a whole building and so you need to track them. No, where are the carpets? How they've been rolled
and things like that. So they're starting to make it a little bit more programmable and of course there is also 20 years ago they started making all this flamboyant architecture with very complex curves and things like that. And that was made possible just because there were programs that enabled that. And so you have parametric the design stuff like that. But in the end, if you just see Autogad and how it is structured, it's heavily
programmatic. And people don't use it like that. People just use it to draw lines. But that's not that's not the real intention of the of Autogut. In Autogat you need to have the information that's important So for me it was natural. I was already inside a little bit of programming. I've done with basic I've done I've done structural analysis. and things like that. It's very easy. You just have to you have a matrix and you have to invert it. That's all. And but you can do very complex things with just little
little knowledge in the end. And another thing that I like is when you connect different things like from Autogad take the informations and pour it into another la language another system and the way back of course. That's I find that very interesting.
Speaker 1
I have a question about this example project it's it's very nice to use to understand how
Andrea Guerra
yes
Speaker 1
how the basics work if you do more complex have you can you avoid having a whole node js stack in place when you're working with maps or
Andrea Guerra
Yes
Speaker 1
a lot of javascript no this is not can you keep
Andrea Guerra
no it's very th this is it's all very basic. You need sqlite You don't you don't need strange backends, you don't need you don't need strange things. But this is done on purpose, on purpose, of course to take it to have it as simple as possible.
Speaker 1
Yeah. But even when you big lar when you've built larger projects you have achieved to keep it
Andrea Guerra
Always. Of course not always. But it's the first thing you think. I want to keep it simple. to manage in the end. So I do the all this for fun. So I don't want to be bothered with strange things that I can't grasp no?
Speaker 1
Yeah. Yeah, keep it fun.
Andrea Guerra
Okay.
Speaker 1
Are there more questions? Otherwise we have the next talk coming up in two minutes. No, wait, no. Break coming up in two minutes. Sorry. Yeah, but let's give a big round of applause for Andrea.
Andrea Guerra
Thank you. Thank you very much