Py.Cafe

iisakkirotko/

solara-issue-21

Plotly Interactive Choropleth Map

DocsPricing
  • app.py
  • requirements.txt
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44

import pandas as pd
import plotly.express as px
import solara

# Generate data
df = pd.DataFrame.from_records(
    [
        {'code': "GBR", "value": 1},
        {'code': "NLD", "value": 10},
        {'code': "USA", "value": 3},
        {'code': "FRA", "value": 2},
        {'code': "ES", "value": 7},
    ]
)

# Make a figure
fig = px.choropleth(data_frame=df, locations='code', color='value')

# Simple solara app that captures clicks on the map and prints the click event:

@solara.component
def Page():
    
    number = solara.use_reactive(0)
    text = solara.use_reactive("text")
    
    def function(x):
        x = str(x)
        text.set(x)

    def increment():
        number.set(number.value + 1)
        x = str(number.value)
        text.set(x)

    
    with solara.Column():
        solara.FigurePlotly(fig=fig, on_click=function)
        solara.Button(on_click=increment)
        solara.Card(title=text.value)
        print(text.value)
        solara.Markdown(text.value)
requirements.txt
1
2
3
4
solara
numpy
pandas
plotly