Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

350
Views
Adding JavaScript to Folium map

I'm working on a project to make a map using folium and flask and I'm trying to add my own javascript to add some animation to the tile to appear one by one. The question is how can I add my custom javascript to the map using python flask

as I have tried this way in this code below:

from branca.element import Element
m = folium.Map()
map_id = m.get_name()

my_js = """
const items = document.querySelectorAll('.leaflet-interactive')
 items.forEach((one) => {
one.style.visibility = 'hidden'
 })
if (items.length > 0) {
if (items.length !== 0) {
  let i = 0
const m = setInterval(function () {
  if (i < items.length) {
    items[i].style.visibility = 'visible'
    i++
  }
  console.log('now i =' + i + ' || the  number of circle = ' + items.length)
  if (i === items.length) {
    clearInterval(m)
    console.log('now cleared')
  }
  }, 1000)
  }
  }
  """.format(map_id)
  e = Element(my_js)
  html = m.get_root()
  html.script.get_root().render()
  # Insert new element or custom JS
  html.script._children[e.get_name()] = e

m.save('mymap.html')

also have tried other way like this:

base_map.get_root().html.add_child(folium.JavascriptLink('static/custom.js'))

it injects to the template's body but it still doesn't work

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

I already found out how to include JavaScript and CSS external link also inline js:

Firstly, way we can add CSS link to the header of the page

m.get_root().header.add_child(CssLink('./static/style.css'))

Then, this is the code to insert JavaScript External link to folium

m.get_root().html.add_child(JavascriptLink('./static/js.js'))

Finally, to add to the Folium script that been generated before

my_js = '''
console.log('working perfectly')
'''
m.get_root().script.add_child(Element(my_js))

resources that helped me to understand how to do this is reading throw branca elements and checking Folium repo

  1. Folium repo
  2. Branca Element
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!