Path: blob/main/docs/_static/flask_example.py
1601 views
"""flask_example.py12Required packages:3- flask4- folium56Usage:78Start the flask server by running:910$ python flask_example.py1112And then head to http://127.0.0.1:5000/ in your browser to see the map displayed1314"""1516from flask import Flask, render_template_string1718import folium1920app = Flask(__name__)212223@app.route("/")24def fullscreen():25"""Simple example of a fullscreen map."""26m = folium.Map()27return m.get_root().render()282930@app.route("/iframe")31def iframe():32"""Embed a map as an iframe on a page."""33m = folium.Map()3435# set the iframe width and height36m.get_root().width = "800px"37m.get_root().height = "600px"38iframe = m.get_root()._repr_html_()3940return render_template_string(41"""42<!DOCTYPE html>43<html>44<head></head>45<body>46<h1>Using an iframe</h1>47{{ iframe|safe }}48</body>49</html>50""",51iframe=iframe,52)535455@app.route("/components")56def components():57"""Extract map components and put those on a page."""58m = folium.Map(59width=800,60height=600,61)6263m.get_root().render()64header = m.get_root().header.render()65body_html = m.get_root().html.render()66script = m.get_root().script.render()6768return render_template_string(69"""70<!DOCTYPE html>71<html>72<head>73{{ header|safe }}74</head>75<body>76<h1>Using components</h1>77{{ body_html|safe }}78<script>79{{ script|safe }}80</script>81</body>82</html>83""",84header=header,85body_html=body_html,86script=script,87)888990if __name__ == "__main__":91app.run(debug=True)929394