Help: Drawing a rectangle on an ipyleaflet map
Trying to create an ipyleaflet component but the state and view management is a bit over my head. Does anyone have experience in creating a component that updates a "bounding_boxes" reactive element?
32 Replies
An interactive example would be the box drawing tool on https://geojson.io
geojson.io
geojson.io | powered by Mapbox
A quick, simple tool for creating, viewing, and sharing spatial data.
Unless there's precedent in solara, I think it may be easier to do this in html/javascript for the purposes of my project.
https://ipyleaflet.readthedocs.io/en/latest/layers/polygon.html#example-editable-polygon
Is this what you mean?
Not exactly. I know that there's something that lets one draw squares or polygons
Im going to look around for it
Ah ok, I think this might be it
https://ipyleaflet.readthedocs.io/en/latest/controls/draw_control.html#ipyleaflet.leaflet.DrawControl.on_draw
Draw Control
It creates a menu like this for drawing various shapes
Upon finishing a shape, It would be cool if there were a reactive variable that is a list of these shapes/geometries.
You can use the
on_draw
callback to write to a reactive variableI've considered this, I think my main issue is that I don't understand how Im supposed to render a map once I've created it. Here's what I have so far:
calling element is what seems to make anything appear.
however it displays a map with now draw control menu:
every example of the ipyleaflet documentation site mentions that I can only add a
draw control
to a map object.pvcastfrontend/02-config.py
line 1103
this is what I do
oh great!
solara documentation mentions they want you to use
.element
on every object, I didn't do that so this is probably not best practiceI would too, but it hasn't been working out for me. I do like the ability to make explicity calls to display.
Thank you for the guidance and answer Stefan @Stefan
GitHub
solara/solara/website/pages/examples/libraries/ipyleaflet_advanced....
A Pure Python, React-style Framework for Scaling Your Jupyter and Web Apps - widgetti/solara
Sorry to join the party so late
I don't know if you already got it working, but something like this also works:
where
draw_control
can hook up to the data using on_draw
and ipyleaflet.DrawControl.data
I didn't really get it working. Im running into new issues:
1) the
on_draw
callback doesn't seem to be called when I draw
the code for that looks like this
2) The polygons drawn on the map disappear when I pan or zoomI'll take a look at that tomorrow, I can't see any immediate reason why that would be
You should store the shapes into a reactive war
data_reactive
and then bind that to the data
attribute of the DrawControl
I'll give this a try, thanks
perhaps it rerenders the map each time or resets the
data
attribute you mentioned. binding a reactive variable to the data attribute might solve both of these, giving it a try
I tried binding it, it did not work. I need to look more into what the binding would look liked (expressed in code).
The solara documentation is pretty sparse which makes me believe that there's another library or paradigm at play that's obvious to seasoned users, but hidden from me.Yeah, this is essentially how it works, with rerendering on map view change
Interesting. I don't have my code, but if I remember correctly
worked for me (should be noted I am working with a custom implementation of the controls, although they should be identical in this respect)
I've tried this but it throws errors. I know that data IS a field, but Im not sure how to instantiate the solara reactive for it.
What I've tried: The error
What I've tried: The error
Ah My bad, made a rookie mistake in that code. It should of course be
data_reactive.value
Where the value is a list
And then data reactive should be set by the on_draw
callbackI see, so
.value
is a mutable reference that the reactive element would still track?
Giving this a try
@Iisakki Rotko that works!
here are the caveats
there is a drawControl
buffer (objects that were drawn), those are always wiped when you update the map. as long as you update the reactive element with your new drawing using on_draw
, what you've drawn will be erased.
Would something like
work?
yeah that's what I do on the callback essentially
Yeah, I was too quick to reply
thank you for your help by the way, I don't think I'd have gotten this far. Now I just need to find an on_delete. but that should be simpler now that I kind of understand how to persist state to a reactive variable.
IIRC there are some issues with using
.append()
on reactive values, so maybe try the code I sent, just in case
No worries, glad to help!