title: Updating folium coordinates on callback in dash app
body: <p>I am trying to update the coordinates of a folium map rendered by a Dash App on a callback (which is running inside a Flask App). The callback selectively renders different layers on a map - the issue is that the zoom and center coordinates are not persisted when the map is updated. The map is rendered as html and injected as in iframe into the app.</p>
  <p><em>Addendum: Not a professional programmer, have only been trying my hand at this for the past six months.</em></p>
  <p>I have tried three approaches:</p>
  <ol>
  <li><p>JS API call client-side to flask route. I ended up realizing this had too much overhead (plus couldn't identify user to update the proper coordinates).</p>
  </li>
  <li><p><strong>Encoding the coordinates and zoom in the URL.</strong> The URL changes as expected from this js snippet:</p>
  </li>
  </ol>
  <pre><code>map_layer.on(&quot;mouseup zoomend&quot;, function(){
                      
     var coordinates = map_layer.getCenter();
     var lat = coordinates.lat
     var lon = coordinates.lng
     var zoom = map_layer.getZoom();
                      
     parent.history.replaceState({}, '', `/app/layer?&amp;lat=${lat}&amp;lon=${lon}&amp;zoom=${zoom}`);
     // const ON_CHANGE = '_dashprivate_historychange';
     // window.dispatchEvent(new CustomEvent(ON_CHANGE));
     // parent.window.dispatchEvent(new CustomEvent(ON_CHANGE));
  
                      
     console.log(&quot;success&quot;);
  </code></pre>
  <p><em>The commented-out code also tries to dispatch a CustomEvent to dash to try and update it's history - not really clear what's happening there - just tried to emulate <a href="https://community.plotly.com/t/react-to-pathname-or-href-change-in-dash-component/55619/3" rel="nofollow noreferrer">this approach</a>.</em></p>
  <p>The right URL is not passed on to the callback however. So despite the url changing on the browser, it's not being sent back with the updated query variables. If I refresh the page and actively send the URL, than the right URL is passed on, but that's not the kind of behavior I'm looking for - I would like for it to change with no active reloading of the page:</p>
  <pre><code>@layer.callback(
      Output(&quot;map&quot;, &quot;srcDoc&quot;),
      -- other inputs --,
      Input('url', 'href') #tried State as well
  )
  def update_output_src(href):
      print(href)
      
      -- other code --
      
      return map.get_root().render()
  </code></pre>
  <ol start="3">
  <li><strong>Using a hidden DIV to store the coordinates</strong>. The div content changes as expected from this js snippet:</li>
  </ol>
  <pre><code>map_layer.on(&quot;mouseup zoomend&quot;, function(){
  
     var coordinates = map_layer.getCenter();
     var lat = coordinates.lat
     var lon = coordinates.lng
     var zoom = map_layer.getZoom();
  
     var latstore = parent.document.getElementById('latstore');
     latstore.textContent = lat; #trying different approaches in what's changed to see if dash callback captures it.
                      
     var lonstore = parent.document.getElementById('lonstore');
     lonstore.innerText = lon;
                      
     var zoomstore = parent.document.getElementById('zoomstore');
     zoomstore.innerHTML = zoom;
  
     console.log(&quot;success&quot;);
  </code></pre>
  <p>But again I am not able to capture the stored coordinates when the input is triggered.</p>
  <pre><code>@layer.callback(
      Output(&quot;map&quot;, &quot;srcDoc&quot;),
      -- other inputs --,
      State('latstore', 'children'),
      State('lonstore', 'children'),
      State('zoomstore', 'children'),
  )
  def update_output_src(latstore, lonstore, zoomstore):
      print(latstore, lonstore, zoomstor)
      
      -- other code --
      
      return map.get_root().render()
  </code></pre>
  <p>Any help or pointer in the right direction for approaches 2 or 3 would be super useful. I have been struggling with this for 3-4 days now and I'm out of ideas.</p>