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

280
Views
How can I add custom HTML to pyqtlet?

My actual problem is much more specific, but I think a more general answer will help me to cope with future needs.

I actually need to enable leaflet-extras plugin for leaflet underlying pyqtlet.

Documentation says:

Download leaflet-providers.js and include it in your page after including Leaflet, e.g.:

<head>
  ...
  <script src="http://unpkg.com/leaflet@1.7.1/dist/leaflet.js"></script>
  <script src="js/leaflet-providers.js"></script>
</head>

and then:

// add Stamen Watercolor to map.
L.tileLayer.provider('Stamen.Watercolor').addTo(map);

Of course all this is in the assumption you are writing a JavaScript app while I am in pyqtlet environment.

I tried to cope in a very ugly way:

  • downloaded leaflet-providers.js to my venv/lib/python3.8/site-packages/pyqtlet/web/modules
  • added the line <script src="modules/leaflet-providers.js"></script> to my venv/lib/python3.8/site-packages/pyqtlet/web/map.html
  • used self.map.runJavaScript("L.tileLayer.provider('Stamen.Watercolor'). addTo(map)") to activate the desired tileLayer

This actually works, but I'm sure there's a cleaner way to achieve the same.

Any hint welcome.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

A trivial solution is to read the .js and run it:

with open("/path/of/leaflet-providers.js") as f:
    self.map.runJavaScript(f.read())
    self.map.runJavaScript("L.tileLayer.provider('Stamen.Watercolor'). addTo(map)")

Another similar option is to use document.createElement:

with open("/path/of/leaflet-providers.js") as f:
    code = f.read()
    SCRIPT = """
    const script = document.createElement('script');
    script.type = 'text/javascript';
    document.head.appendChild(script);
    script.text = `%s`;
    """ % (
        code,
    )
    self.map.runJavaScript(SCRIPT)
    self.map.runJavaScript(
        "L.tileLayer.provider('Stamen.Watercolor'). addTo(map)"
    )

enter image description here

about 4 years ago · Juan Pablo Isaza 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!