CARTOframes

A Python package for integrating CARTO maps, analysis, and data services into data science workflows.

Popups

Popups are useful for interactively displaying additional information about a feature on a map. The events that allow us to interact with the feature are hover and click. A feature can listen to both events and display different information for each one.

1
2
3
4
from cartoframes.auth import set_default_context
from cartoframes.viz import Map, Layer

set_default_context('https://cartovl.carto.com/')

Basic popups

1
2
3
4
5
6
7
8
9
10
Map(
    Layer(
        'sf_neighborhoods',
        'color: ramp(globalQuantiles($cartodb_id, 5), purpor)',
        {
            'hover': '$name',
            'click': ['$name', '$created_at']
        }
    )
)

Set Popup for 'hover' and 'click' events

### Using expressions

1
2
3
4
5
6
7
8
9
10
Map(
    Layer(
        'populated_places',
        'width: 15',
        {
            'hover': ['sqrt($pop_max)', '$pop_min % 100']
        }
    ),
    viewport={'zoom': 3.89, 'lat': 39.90, 'lng': 5.52}
)

Set Popup for 'hover' events using expressions

Adding titles

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
from cartoframes.viz import Popup

Map(
    Layer(
        'sf_neighborhoods',
        popup=Popup({
            'hover': {
                'title': 'Name',
                'value': '$name'
            },
            'click': [{
                'title': 'Name',
                'value': '$name'
            },{
                'title': 'Created at',
                'value': '$created_at'
            }]
        })
    )
)

Set Popup title and value for 'hover' and 'click' events