Skip to content

Commit

Permalink
Fix leaflet (#742)
Browse files Browse the repository at this point in the history
* Fix leaflet

* Add br to accept encoding
  • Loading branch information
almarklein committed Jan 6, 2024
1 parent 7461524 commit ce1eb56
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions flexxamples/howtos/leaflet.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@


import os
from urllib.request import urlopen, Request
import re
import base64
import mimetypes

import requests # Note: you may need to "pip install requests brotli"
import brotli # noqa: requests needs this for br encoding
from flexx import flx


Expand All @@ -21,6 +22,10 @@
'marker-shadow.png',
]

request_headers = {
'User-Agent': 'flexx/%s' % flx.__version__,
'Accept-Encoding': 'gzip, deflate, br'
}

if 'LEAFLET_DIR' in os.environ:
_base_url = 'file://%s' % os.environ['LEAFLET_DIR']
Expand All @@ -33,8 +38,9 @@ def _get_code(item):
""" Get a text item from _base_url
"""
url = '%s/%s' % (_base_url, item)
req = Request(url, headers={'User-Agent': 'flexx/%s' % flx.__version__})
return urlopen(req).read().decode()
res = requests.get(url , headers=request_headers)
assert res.ok, f"{res.status_code}: {res.reason}"
return res.text


def _get_data(item_or_url):
Expand All @@ -44,8 +50,9 @@ def _get_data(item_or_url):
url = item_or_url
else:
url = '%s/%s' % (_base_url, item_or_url)
req = Request(url, headers={'User-Agent': 'flexx/%s' % flx.__version__})
return urlopen(req).read()
res = requests.get(url , headers=request_headers)
assert res.ok, f"{res.status_code}: {res.reason}"
return res.content


def _embed_css_resources(css, types=('.png',)):
Expand Down Expand Up @@ -281,5 +288,5 @@ def handle_leaflet_mouse(self, *events):


if __name__ == '__main__':
flx.launch(LeafletExample, 'firefox')
flx.launch(LeafletExample, 'chrome-browser')
flx.run()

0 comments on commit ce1eb56

Please sign in to comment.