"<h1><span style=\"color:green\"> Tetras-Lab for interactive geographical data visualisation</span></h1>\n",
"<h1><span style=\"color:green\"> Tetras-Lab for interactive geographical data visualisation using Dash Leaflet</span></h1>\n",
"\n",
"\n",
"In this galery you will learn how to create dashboards using the open source data intelligence platform Tetras-Lab and easily visualize and share them as a web app.\n",
"In this galery you will learn how to create dashboards using the open source data intelligence platform Tetras-Lab and easily visualize and share them as a web app.\n",
"\n",
"\n",
...
@@ -77,6 +76,7 @@
...
@@ -77,6 +76,7 @@
"\n",
"\n",
"The following imports are gonna be required : \n",
"The following imports are gonna be required : \n",
"Then we can add the components we want to the app layout, each component works like a HTML element to which we can set a CSS style attribute to manage its size, color, font, etc...\n",
"Then we can add the components we want to the app layout, each component works like a HTML element to which we can set a CSS style attribute to manage its size, color, font, etc...\n",
"\n",
"\n",
"Exemple for a dropdown selector, first we declare the component : \n",
"Exemple for a dropdown selector, first we declare the component : \n",
"We will now use the Dash_Leaflet Library to display an interactive map, the component to use is ```dl.Map()``` , without adding anything else, the only thing that will be displayed is a grey square.\n",
"We will now use the Dash_Leaflet Library to display an interactive map, the component to use is ```dl.Map()``` , without adding anything else, the only thing that will be displayed is a grey square.\n",
"We need to draw some map tiles, by adding ``` dl.TileLayer()``` as an argument, Dash Leaflet will add a default map layer based on OpenStreetMap.\n",
"We need to draw some map tiles, by adding ``` dl.TileLayer()``` as an argument, Dash Leaflet will add a default map layer based on OpenStreetMap.\n",
"\n",
"\n",
"\n",
"```Python\n",
"```Python\n",
"mapComponent = dl.Map(children=\n",
"mapComponent = dl.Map(children=\n",
" [dl.TileLayer()],\n",
" [dl.TileLayer()],\n",
...
@@ -263,6 +272,7 @@
...
@@ -263,6 +272,7 @@
"\n",
"\n",
"The ```children``` attribute of the map component allows us to add different kinds of layers to the map such as markers, polygons, and many others.\n",
"The ```children``` attribute of the map component allows us to add different kinds of layers to the map such as markers, polygons, and many others.\n",
"\n",
"\n",
"\n",
"```Python\n",
"```Python\n",
"marker = dl.Marker(position = [46.5,2.25], id ='marker')\n",
"marker = dl.Marker(position = [46.5,2.25], id ='marker')\n",
"We assign an id to each component because the callbacks which we will define in the next part will use them to refer to the components we want to use as inputs or outputs."
"We assign an id to each component because the callbacks which we will define in the next part will use them to refer to the components we want to use as inputs or outputs."
]
]
},
},
...
@@ -335,21 +346,28 @@
...
@@ -335,21 +346,28 @@
"source": [
"source": [
"<h2><span style=\"color:green\">Interactivity using callbacks </span></h2>\n",
"<h2><span style=\"color:green\">Interactivity using callbacks </span></h2>\n",
"\n",
"\n",
"Once we have a map and know how to display elements on it, we would like to be able to click on a map component and have its information ( e.g the name of a country ) retrieved outside the map and usable in our program.\n",
"\n",
"A Dash callback is made of 2 parts : A function that will be called when a specified component is updated, and a function decorator which will define the inputs : the variables the app is gonna watch for updates, and the output : the component that's gonna be changed when the function is called. \n",
"A Dash callback is made of 2 parts : A function that will be called when a specified component is updated, and a function decorator which will define the inputs : the variables the app is gonna watch for updates, and the output : the component that's gonna be changed when the function is called. \n",
"This decorator means the app will be looking for any updates to the number of times the marker has been clicked, and that the result of the following function will be sent to the component with the 'result' id.\n",
"This decorator means the app will be looking for any updates to the number of times the marker has been clicked, and that the result of the following function will be sent to the component with the 'result' id.\n",
"\n",
"\n",
"\n",
"```Python\n",
"```Python\n",
"def print_click(n):\n",
"def print_click(n):\n",
" return ('Marker clicks : ' + str(n))\n",
" return ('Marker clicks : ' + str(n))\n",
"``` \n",
"``` \n",
"\n",
"\n",
"\n",
"The print_click function will be called every time the app detects an update on the 'n_clicks' component of the marker.\n",
"The print_click function will be called every time the app detects an update on the 'n_clicks' component of the marker.\n",
"\n",
"\n",
"\n",
"```Python \n",
"```Python \n",
"txt_output = html.Div(id='result')\n",
"txt_output = html.Div(id='result')\n",
"app.layout = html.Div(children = (txt_output, mapComponent)) #Just like the map component, to have multiple components in our layout they have to be in a list in the children attribute.\n",
"app.layout = html.Div(children = (txt_output, mapComponent)) #Just like the map component, to have multiple components in our layout they have to be in a list in the children attribute.\n",
"txt_output = html.Div(id='result',children='Click the marker !', style={'float': 'left','margin': 'auto','color':'green'})\n",
"\n",
"\n",
"mapComponent = dl.Map(children = \n",
"mapComponent = dl.Map(children = \n",
" [dl.TileLayer(),marker],\n",
" [dl.TileLayer(),marker],\n",
...
@@ -422,6 +440,7 @@
...
@@ -422,6 +440,7 @@
"\n",
"\n",
"The function decorator may also have a third kind of argument : States. Just like inputs they are entry variables to the function, but updating a State will not trigger the callback, their purpose is to give information on the state of the app.\n",
"The function decorator may also have a third kind of argument : States. Just like inputs they are entry variables to the function, but updating a State will not trigger the callback, their purpose is to give information on the state of the app.\n",
"Here we choose a color for the polygon with the selector, its values is a State, but the change only happens once the button is clicked because it is the input."
"Here we choose a color for the polygon with the selector, its values is a State, but the change only happens once the button is clicked because it is the input."
...
@@ -525,6 +545,7 @@
...
@@ -525,6 +545,7 @@
"\n",
"\n",
"Here with 2 different GeoJSON files : departements.geojson for France's departments and regions.geojson for France's regions we can create a toggle which will make the geojson layer display either the regions or the departements.\n",
"Here with 2 different GeoJSON files : departements.geojson for France's departments and regions.geojson for France's regions we can create a toggle which will make the geojson layer display either the regions or the departements.\n",
"\n",
"\n",
"\n",
"```Python\n",
"```Python\n",
"geojson = dl.GeoJSON(url=base_path+'assets/regions.geojson', # url to geojson file\n",
"geojson = dl.GeoJSON(url=base_path+'assets/regions.geojson', # url to geojson file\n",
" zoomToBounds=True, # when true, zooms to bounds when data changes (e.g. on load)\n",
" zoomToBounds=True, # when true, zooms to bounds when data changes (e.g. on load)\n",
...
@@ -550,9 +571,12 @@
...
@@ -550,9 +571,12 @@
"def switch_file(value):\n",
"def switch_file(value):\n",
" return (base_path+'assets/' + value + 's.geojson')\n",
" return (base_path+'assets/' + value + 's.geojson')\n",
"```\n",
"```\n",
"\n",
"\n",
"So the GeoJSON layer works as a callback output, but it can also work as an input. \n",
"So the GeoJSON layer works as a callback output, but it can also work as an input. \n",
"Each polygon drawn in the layer has different features such as the `click_feature` which updates when any of the polygon is clicked and allows us to get informations on it.\n",
"Each polygon drawn in the layer has different features such as the `click_feature` which updates when any of the polygon is clicked and allows us to get informations on it.\n",
<h1><spanstyle="color:green"> Tetras-Lab for interactive geographical data visualisation</span></h1>
<h1><spanstyle="color:green"> Tetras-Lab for interactive geographical data visualisation using Dash Leaflet</span></h1>
In this galery you will learn how to create dashboards using the open source data intelligence platform Tetras-Lab and easily visualize and share them as a web app.
In this galery you will learn how to create dashboards using the open source data intelligence platform Tetras-Lab and easily visualize and share them as a web app.
<h2><spanstyle="color:green"> Set up </span></h2>
<h2><spanstyle="color:green"> Set up </span></h2>
We will be using the python libraries Dash and Dash-Leaflet, which can be easily added to Tetras-Lab, to create the components to be displayed in our dashboard.
We will be using the python libraries Dash and Dash-Leaflet, which can be easily added to Tetras-Lab, to create the components to be displayed in our dashboard.
The following imports are gonna be required :
The following imports are gonna be required :
```Python
```Python
from dash import html
from dash import html
import dash_leaflet as dl
import dash_leaflet as dl
import dash_leaflet.express as dlx
import dash_leaflet.express as dlx
from jupyter_dash import JupyterDash as Dash
from jupyter_dash import JupyterDash as Dash
from dash_extensions.javascript import arrow_function, assign
from dash_extensions.javascript import arrow_function, assign
from dash_extensions.enrich import Output, DashProxy, Input, MultiplexerTransform, State
from dash_extensions.enrich import Output, DashProxy, Input, MultiplexerTransform, State
Then we can add the components we want to the app layout, each component works like a HTML element to which we can set a CSS style attribute to manage its size, color, font, etc...
Then we can add the components we want to the app layout, each component works like a HTML element to which we can set a CSS style attribute to manage its size, color, font, etc...
Exemple for a dropdown selector, first we declare the component :
Exemple for a dropdown selector, first we declare the component :
```Python
```Python
dropdown = dcc.Dropdown(
dropdown = dcc.Dropdown(
id='demo-dropdown',
id='demo-dropdown',
options=[
options=[
{'label': 'Paris', 'value': 'paris'},
{'label': 'Paris', 'value': 'paris'},
{'label': 'Lyon', 'value': 'lyon'},
{'label': 'Lyon', 'value': 'lyon'},
{'label': 'Marseille', 'value': 'marseille'},
{'label': 'Marseille', 'value': 'marseille'},
{'label': 'Grenoble', 'value': 'grenoble'}
{'label': 'Grenoble', 'value': 'grenoble'}
],
],
value='paris' #The value that will be intially selected
value='paris' #The value that will be intially selected
We will now use the Dash_Leaflet Library to display an interactive map, the component to use is ```dl.Map()``` , without adding anything else, the only thing that will be displayed is a grey square.
We will now use the Dash_Leaflet Library to display an interactive map, the component to use is ```dl.Map()``` , without adding anything else, the only thing that will be displayed is a grey square.
We need to draw some map tiles, by adding ``` dl.TileLayer()``` as an argument, Dash Leaflet will add a default map layer based on OpenStreetMap.
We need to draw some map tiles, by adding ``` dl.TileLayer()``` as an argument, Dash Leaflet will add a default map layer based on OpenStreetMap.
We assign an id to each component because the callbacks which we will define in the next part will use them to refer to the components we want to use as inputs or outputs.
We assign an id to each component because the callbacks which we will define in the next part will use them to refer to the components we want to use as inputs or outputs.
<h2><spanstyle="color:green">Interactivity using callbacks </span></h2>
<h2><spanstyle="color:green">Interactivity using callbacks </span></h2>
Once we have a map and know how to display elements on it, we would like to be able to click on a map component and have its information ( e.g the name of a country ) retrieved outside the map and usable in our program.
A Dash callback is made of 2 parts : A function that will be called when a specified component is updated, and a function decorator which will define the inputs : the variables the app is gonna watch for updates, and the output : the component that's gonna be changed when the function is called.
A Dash callback is made of 2 parts : A function that will be called when a specified component is updated, and a function decorator which will define the inputs : the variables the app is gonna watch for updates, and the output : the component that's gonna be changed when the function is called.
This decorator means the app will be looking for any updates to the number of times the marker has been clicked, and that the result of the following function will be sent to the component with the 'result' id.
This decorator means the app will be looking for any updates to the number of times the marker has been clicked, and that the result of the following function will be sent to the component with the 'result' id.
```Python
```Python
def print_click(n):
def print_click(n):
return ('Marker clicks : ' + str(n))
return ('Marker clicks : ' + str(n))
```
```
The print_click function will be called every time the app detects an update on the 'n_clicks' component of the marker.
The print_click function will be called every time the app detects an update on the 'n_clicks' component of the marker.
```Python
```Python
txt_output = html.Div(id='result')
txt_output = html.Div(id='result')
app.layout = html.Div(children = (txt_output, mapComponent)) #Just like the map component, to have multiple components in our layout they have to be in a list in the children attribute.
app.layout = html.Div(children = (txt_output, mapComponent)) #Just like the map component, to have multiple components in our layout they have to be in a list in the children attribute.
A callback may have as many inputs and outputs as needed, the linked function will be called when any one of the inputs is updated.
A callback may have as many inputs and outputs as needed, the linked function will be called when any one of the inputs is updated.
But two different callbacks can not have the same output, this is to prevent overlapping issues that could happen if the two callbacks are triggered at the same time.
But two different callbacks can not have the same output, this is to prevent overlapping issues that could happen if the two callbacks are triggered at the same time.
The function decorator may also have a third kind of argument : States. Just like inputs they are entry variables to the function, but updating a State will not trigger the callback, their purpose is to give information on the state of the app.
The function decorator may also have a third kind of argument : States. Just like inputs they are entry variables to the function, but updating a State will not trigger the callback, their purpose is to give information on the state of the app.
Here we choose a color for the polygon with the selector, its values is a State, but the change only happens once the button is clicked because it is the input.
Here we choose a color for the polygon with the selector, its values is a State, but the change only happens once the button is clicked because it is the input.
The Dash_Leaflet library offers the possibility to create map layers based on GeoJSON files.
The Dash_Leaflet library offers the possibility to create map layers based on GeoJSON files.
`dl.GeoJSON` works just like other layers we have seen before. We can give the layer an id and use its different attributes inside callbacks.
`dl.GeoJSON` works just like other layers we have seen before. We can give the layer an id and use its different attributes inside callbacks.
Here with 2 different GeoJSON files : departements.geojson for France's departments and regions.geojson for France's regions we can create a toggle which will make the geojson layer display either the regions or the departements.
Here with 2 different GeoJSON files : departements.geojson for France's departments and regions.geojson for France's regions we can create a toggle which will make the geojson layer display either the regions or the departements.
```Python
```Python
geojson = dl.GeoJSON(url=base_path+'assets/regions.geojson', # url to geojson file
geojson = dl.GeoJSON(url=base_path+'assets/regions.geojson', # url to geojson file
zoomToBounds=True, # when true, zooms to bounds when data changes (e.g. on load)
zoomToBounds=True, # when true, zooms to bounds when data changes (e.g. on load)
zoomToBoundsOnClick=True, # when true, zooms to bounds of feature (e.g. polygon) on click
zoomToBoundsOnClick=True, # when true, zooms to bounds of feature (e.g. polygon) on click
return (base_path+'assets/' + value + 's.geojson')
return (base_path+'assets/' + value + 's.geojson')
```
```
So the GeoJSON layer works as a callback output, but it can also work as an input.
So the GeoJSON layer works as a callback output, but it can also work as an input.
Each polygon drawn in the layer has different features such as the `click_feature` which updates when any of the polygon is clicked and allows us to get informations on it.
Each polygon drawn in the layer has different features such as the `click_feature` which updates when any of the polygon is clicked and allows us to get informations on it.
"<h1><span style=\"color:green\"> Tetras-Lab for interactive geographical data visualisation</span></h1>\n",
"<h1><span style=\"color:green\"> Tetras-Lab for interactive geographical data visualisation using Dash Leaflet</span></h1>\n",
"\n",
"\n",
"In this galery you will learn how to create dashboards using the open source data intelligence platform Tetras-Lab and easily visualize and share them as a web app.\n",
"In this galery you will learn how to create dashboards using the open source data intelligence platform Tetras-Lab and easily visualize and share them as a web app.\n",
"\n",
"\n",
...
@@ -77,6 +76,7 @@
...
@@ -77,6 +76,7 @@
"\n",
"\n",
"The following imports are gonna be required : \n",
"The following imports are gonna be required : \n",
"Then we can add the components we want to the app layout, each component works like a HTML element to which we can set a CSS style attribute to manage its size, color, font, etc...\n",
"Then we can add the components we want to the app layout, each component works like a HTML element to which we can set a CSS style attribute to manage its size, color, font, etc...\n",
"\n",
"\n",
"Exemple for a dropdown selector, first we declare the component : \n",
"Exemple for a dropdown selector, first we declare the component : \n",
"We will now use the Dash_Leaflet Library to display an interactive map, the component to use is ```dl.Map()``` , without adding anything else, the only thing that will be displayed is a grey square.\n",
"We will now use the Dash_Leaflet Library to display an interactive map, the component to use is ```dl.Map()``` , without adding anything else, the only thing that will be displayed is a grey square.\n",
"We need to draw some map tiles, by adding ``` dl.TileLayer()``` as an argument, Dash Leaflet will add a default map layer based on OpenStreetMap.\n",
"We need to draw some map tiles, by adding ``` dl.TileLayer()``` as an argument, Dash Leaflet will add a default map layer based on OpenStreetMap.\n",
"\n",
"\n",
"\n",
"```Python\n",
"```Python\n",
"mapComponent = dl.Map(children=\n",
"mapComponent = dl.Map(children=\n",
" [dl.TileLayer()],\n",
" [dl.TileLayer()],\n",
...
@@ -263,6 +272,7 @@
...
@@ -263,6 +272,7 @@
"\n",
"\n",
"The ```children``` attribute of the map component allows us to add different kinds of layers to the map such as markers, polygons, and many others.\n",
"The ```children``` attribute of the map component allows us to add different kinds of layers to the map such as markers, polygons, and many others.\n",
"\n",
"\n",
"\n",
"```Python\n",
"```Python\n",
"marker = dl.Marker(position = [46.5,2.25], id ='marker')\n",
"marker = dl.Marker(position = [46.5,2.25], id ='marker')\n",
"We assign an id to each component because the callbacks which we will define in the next part will use them to refer to the components we want to use as inputs or outputs."
"We assign an id to each component because the callbacks which we will define in the next part will use them to refer to the components we want to use as inputs or outputs."
]
]
},
},
...
@@ -335,21 +346,28 @@
...
@@ -335,21 +346,28 @@
"source": [
"source": [
"<h2><span style=\"color:green\">Interactivity using callbacks </span></h2>\n",
"<h2><span style=\"color:green\">Interactivity using callbacks </span></h2>\n",
"\n",
"\n",
"Once we have a map and know how to display elements on it, we would like to be able to click on a map component and have its information ( e.g the name of a country ) retrieved outside the map and usable in our program.\n",
"\n",
"A Dash callback is made of 2 parts : A function that will be called when a specified component is updated, and a function decorator which will define the inputs : the variables the app is gonna watch for updates, and the output : the component that's gonna be changed when the function is called. \n",
"A Dash callback is made of 2 parts : A function that will be called when a specified component is updated, and a function decorator which will define the inputs : the variables the app is gonna watch for updates, and the output : the component that's gonna be changed when the function is called. \n",
"This decorator means the app will be looking for any updates to the number of times the marker has been clicked, and that the result of the following function will be sent to the component with the 'result' id.\n",
"This decorator means the app will be looking for any updates to the number of times the marker has been clicked, and that the result of the following function will be sent to the component with the 'result' id.\n",
"\n",
"\n",
"\n",
"```Python\n",
"```Python\n",
"def print_click(n):\n",
"def print_click(n):\n",
" return ('Marker clicks : ' + str(n))\n",
" return ('Marker clicks : ' + str(n))\n",
"``` \n",
"``` \n",
"\n",
"\n",
"\n",
"The print_click function will be called every time the app detects an update on the 'n_clicks' component of the marker.\n",
"The print_click function will be called every time the app detects an update on the 'n_clicks' component of the marker.\n",
"\n",
"\n",
"\n",
"```Python \n",
"```Python \n",
"txt_output = html.Div(id='result')\n",
"txt_output = html.Div(id='result')\n",
"app.layout = html.Div(children = (txt_output, mapComponent)) #Just like the map component, to have multiple components in our layout they have to be in a list in the children attribute.\n",
"app.layout = html.Div(children = (txt_output, mapComponent)) #Just like the map component, to have multiple components in our layout they have to be in a list in the children attribute.\n",
"txt_output = html.Div(id='result',children='Click the marker !', style={'float': 'left','margin': 'auto','color':'green'})\n",
"\n",
"\n",
"mapComponent = dl.Map(children = \n",
"mapComponent = dl.Map(children = \n",
" [dl.TileLayer(),marker],\n",
" [dl.TileLayer(),marker],\n",
...
@@ -422,6 +440,7 @@
...
@@ -422,6 +440,7 @@
"\n",
"\n",
"The function decorator may also have a third kind of argument : States. Just like inputs they are entry variables to the function, but updating a State will not trigger the callback, their purpose is to give information on the state of the app.\n",
"The function decorator may also have a third kind of argument : States. Just like inputs they are entry variables to the function, but updating a State will not trigger the callback, their purpose is to give information on the state of the app.\n",
"Here we choose a color for the polygon with the selector, its values is a State, but the change only happens once the button is clicked because it is the input."
"Here we choose a color for the polygon with the selector, its values is a State, but the change only happens once the button is clicked because it is the input."
...
@@ -525,6 +545,7 @@
...
@@ -525,6 +545,7 @@
"\n",
"\n",
"Here with 2 different GeoJSON files : departements.geojson for France's departments and regions.geojson for France's regions we can create a toggle which will make the geojson layer display either the regions or the departements.\n",
"Here with 2 different GeoJSON files : departements.geojson for France's departments and regions.geojson for France's regions we can create a toggle which will make the geojson layer display either the regions or the departements.\n",
"\n",
"\n",
"\n",
"```Python\n",
"```Python\n",
"geojson = dl.GeoJSON(url=base_path+'assets/regions.geojson', # url to geojson file\n",
"geojson = dl.GeoJSON(url=base_path+'assets/regions.geojson', # url to geojson file\n",
" zoomToBounds=True, # when true, zooms to bounds when data changes (e.g. on load)\n",
" zoomToBounds=True, # when true, zooms to bounds when data changes (e.g. on load)\n",
...
@@ -550,9 +571,12 @@
...
@@ -550,9 +571,12 @@
"def switch_file(value):\n",
"def switch_file(value):\n",
" return (base_path+'assets/' + value + 's.geojson')\n",
" return (base_path+'assets/' + value + 's.geojson')\n",
"```\n",
"```\n",
"\n",
"\n",
"So the GeoJSON layer works as a callback output, but it can also work as an input. \n",
"So the GeoJSON layer works as a callback output, but it can also work as an input. \n",
"Each polygon drawn in the layer has different features such as the `click_feature` which updates when any of the polygon is clicked and allows us to get informations on it.\n",
"Each polygon drawn in the layer has different features such as the `click_feature` which updates when any of the polygon is clicked and allows us to get informations on it.\n",
<h1><spanstyle="color:green"> Tetras-Lab for interactive geographical data visualisation</span></h1>
<h1><spanstyle="color:green"> Tetras-Lab for interactive geographical data visualisation using Dash Leaflet</span></h1>
In this galery you will learn how to create dashboards using the open source data intelligence platform Tetras-Lab and easily visualize and share them as a web app.
In this galery you will learn how to create dashboards using the open source data intelligence platform Tetras-Lab and easily visualize and share them as a web app.
<h2><spanstyle="color:green"> Set up </span></h2>
<h2><spanstyle="color:green"> Set up </span></h2>
We will be using the python libraries Dash and Dash-Leaflet, which can be easily added to Tetras-Lab, to create the components to be displayed in our dashboard.
We will be using the python libraries Dash and Dash-Leaflet, which can be easily added to Tetras-Lab, to create the components to be displayed in our dashboard.
The following imports are gonna be required :
The following imports are gonna be required :
```Python
```Python
from dash import html
from dash import html
import dash_leaflet as dl
import dash_leaflet as dl
import dash_leaflet.express as dlx
import dash_leaflet.express as dlx
from jupyter_dash import JupyterDash as Dash
from jupyter_dash import JupyterDash as Dash
from dash_extensions.javascript import arrow_function, assign
from dash_extensions.javascript import arrow_function, assign
from dash_extensions.enrich import Output, DashProxy, Input, MultiplexerTransform, State
from dash_extensions.enrich import Output, DashProxy, Input, MultiplexerTransform, State
Then we can add the components we want to the app layout, each component works like a HTML element to which we can set a CSS style attribute to manage its size, color, font, etc...
Then we can add the components we want to the app layout, each component works like a HTML element to which we can set a CSS style attribute to manage its size, color, font, etc...
Exemple for a dropdown selector, first we declare the component :
Exemple for a dropdown selector, first we declare the component :
```Python
```Python
dropdown = dcc.Dropdown(
dropdown = dcc.Dropdown(
id='demo-dropdown',
id='demo-dropdown',
options=[
options=[
{'label': 'Paris', 'value': 'paris'},
{'label': 'Paris', 'value': 'paris'},
{'label': 'Lyon', 'value': 'lyon'},
{'label': 'Lyon', 'value': 'lyon'},
{'label': 'Marseille', 'value': 'marseille'},
{'label': 'Marseille', 'value': 'marseille'},
{'label': 'Grenoble', 'value': 'grenoble'}
{'label': 'Grenoble', 'value': 'grenoble'}
],
],
value='paris' #The value that will be intially selected
value='paris' #The value that will be intially selected
We will now use the Dash_Leaflet Library to display an interactive map, the component to use is ```dl.Map()``` , without adding anything else, the only thing that will be displayed is a grey square.
We will now use the Dash_Leaflet Library to display an interactive map, the component to use is ```dl.Map()``` , without adding anything else, the only thing that will be displayed is a grey square.
We need to draw some map tiles, by adding ``` dl.TileLayer()``` as an argument, Dash Leaflet will add a default map layer based on OpenStreetMap.
We need to draw some map tiles, by adding ``` dl.TileLayer()``` as an argument, Dash Leaflet will add a default map layer based on OpenStreetMap.
We assign an id to each component because the callbacks which we will define in the next part will use them to refer to the components we want to use as inputs or outputs.
We assign an id to each component because the callbacks which we will define in the next part will use them to refer to the components we want to use as inputs or outputs.
<h2><spanstyle="color:green">Interactivity using callbacks </span></h2>
<h2><spanstyle="color:green">Interactivity using callbacks </span></h2>
Once we have a map and know how to display elements on it, we would like to be able to click on a map component and have its information ( e.g the name of a country ) retrieved outside the map and usable in our program.
A Dash callback is made of 2 parts : A function that will be called when a specified component is updated, and a function decorator which will define the inputs : the variables the app is gonna watch for updates, and the output : the component that's gonna be changed when the function is called.
A Dash callback is made of 2 parts : A function that will be called when a specified component is updated, and a function decorator which will define the inputs : the variables the app is gonna watch for updates, and the output : the component that's gonna be changed when the function is called.
This decorator means the app will be looking for any updates to the number of times the marker has been clicked, and that the result of the following function will be sent to the component with the 'result' id.
This decorator means the app will be looking for any updates to the number of times the marker has been clicked, and that the result of the following function will be sent to the component with the 'result' id.
```Python
```Python
def print_click(n):
def print_click(n):
return ('Marker clicks : ' + str(n))
return ('Marker clicks : ' + str(n))
```
```
The print_click function will be called every time the app detects an update on the 'n_clicks' component of the marker.
The print_click function will be called every time the app detects an update on the 'n_clicks' component of the marker.
```Python
```Python
txt_output = html.Div(id='result')
txt_output = html.Div(id='result')
app.layout = html.Div(children = (txt_output, mapComponent)) #Just like the map component, to have multiple components in our layout they have to be in a list in the children attribute.
app.layout = html.Div(children = (txt_output, mapComponent)) #Just like the map component, to have multiple components in our layout they have to be in a list in the children attribute.
A callback may have as many inputs and outputs as needed, the linked function will be called when any one of the inputs is updated.
A callback may have as many inputs and outputs as needed, the linked function will be called when any one of the inputs is updated.
But two different callbacks can not have the same output, this is to prevent overlapping issues that could happen if the two callbacks are triggered at the same time.
But two different callbacks can not have the same output, this is to prevent overlapping issues that could happen if the two callbacks are triggered at the same time.
The function decorator may also have a third kind of argument : States. Just like inputs they are entry variables to the function, but updating a State will not trigger the callback, their purpose is to give information on the state of the app.
The function decorator may also have a third kind of argument : States. Just like inputs they are entry variables to the function, but updating a State will not trigger the callback, their purpose is to give information on the state of the app.
Here we choose a color for the polygon with the selector, its values is a State, but the change only happens once the button is clicked because it is the input.
Here we choose a color for the polygon with the selector, its values is a State, but the change only happens once the button is clicked because it is the input.
The Dash_Leaflet library offers the possibility to create map layers based on GeoJSON files.
The Dash_Leaflet library offers the possibility to create map layers based on GeoJSON files.
`dl.GeoJSON` works just like other layers we have seen before. We can give the layer an id and use its different attributes inside callbacks.
`dl.GeoJSON` works just like other layers we have seen before. We can give the layer an id and use its different attributes inside callbacks.
Here with 2 different GeoJSON files : departements.geojson for France's departments and regions.geojson for France's regions we can create a toggle which will make the geojson layer display either the regions or the departements.
Here with 2 different GeoJSON files : departements.geojson for France's departments and regions.geojson for France's regions we can create a toggle which will make the geojson layer display either the regions or the departements.
```Python
```Python
geojson = dl.GeoJSON(url=base_path+'assets/regions.geojson', # url to geojson file
geojson = dl.GeoJSON(url=base_path+'assets/regions.geojson', # url to geojson file
zoomToBounds=True, # when true, zooms to bounds when data changes (e.g. on load)
zoomToBounds=True, # when true, zooms to bounds when data changes (e.g. on load)
zoomToBoundsOnClick=True, # when true, zooms to bounds of feature (e.g. polygon) on click
zoomToBoundsOnClick=True, # when true, zooms to bounds of feature (e.g. polygon) on click
return (base_path+'assets/' + value + 's.geojson')
return (base_path+'assets/' + value + 's.geojson')
```
```
So the GeoJSON layer works as a callback output, but it can also work as an input.
So the GeoJSON layer works as a callback output, but it can also work as an input.
Each polygon drawn in the layer has different features such as the `click_feature` which updates when any of the polygon is clicked and allows us to get informations on it.
Each polygon drawn in the layer has different features such as the `click_feature` which updates when any of the polygon is clicked and allows us to get informations on it.