Custom Tile Server

This example implements a minimal map tile server that serves map tiles from two different basemaps in a chess board pattern.


In [ ]:
from multiprocessing import Process

from flask import Flask, Response
from requests import get as fetch
from ipyleaflet import Map, basemaps

Build and Run a Tile Server


In [ ]:
app = Flask(__name__)

@app.route("/scramble/<int:z>/<int:x>/<int:y>")
def scramble(z, x, y):
    "Serve scrambled tiles from two basemaps."

    urls = [basemaps.Esri[name]['url'] for name in ['WorldStreetMap', 'WorldTopoMap']]
    url = urls[(x + y) % 2].format(x=x, y=y, z=z)
    img = fetch(url).content
    return Response(img, mimetype='image/png')

In [ ]:
p = Process(target=app.run)
p.start()

Show a "Scrambled Tiles" Map


In [ ]:
url = 'http://localhost:5000/scramble/{z}/{x}/{y}'
bm = {'url': url, 'attribution': 'Scrambled Tiles'}
Map(center=(48.86, 2.34), level=10, basemap=bm)

Stop the Tile Server


In [ ]:
p.kill()