cartoframes.viz.layer module

class cartoframes.viz.layer.Layer(source, style=None, popup=None, legend=None, context=None)

Bases: object

:param source (str, Dataset,: Source): The source data. :param style (str, dict, Style,: optional): The style of the visualization: `CARTO VL styling

Parameters:
  • popup (dict, Popup, optional) – This option adds interactivity (click and hover) to a layer to show popups. The columns to be shown must be added in a list format for each event. It must be written using CARTO VL expressions syntax <https://carto.com/developers/carto-vl/reference/#cartoexpressions>.
  • legend (dict, Legend, optional) – The legend definition for a layer. It contains the information to show a legend “type” (color-category, color-bins, color-continuous), “prop” (color) and also text information: “title”, “description” and “footer”.
  • context (Context) – A Context instance. This is only used for the simplified Source API. When a Source is pased as source, this context is simply ignored. If not provided the context will be automatically obtained from the default context.

Example

from cartoframes.auth import set_default_context
from cartoframes.viz import Layer

set_default_context(
    base_url='https://cartovl.carto.com/',
    api_key='default_public'
)

Layer(
    'SELECT * FROM populated_places WHERE adm0name = 'Spain'',
    'color: ramp(globalQuantiles($pop_max, 5), reverse(purpor))',
    popup={
        'hover': '$name',
        'click': ['$name', '$pop_max', '$pop_min']
    },
    legend={
        'type': 'color-category',
        'prop': 'color',
        'title': 'Population'
    }
)

Setting the context.

from cartoframes.auth import Context
from cartoframes.viz import Layer

context = Context(
    base_url='https://your_user_name.carto.com',
    api_key='your api key'
)

Layer(
    'populated_places',
    'color: "red"',
    context=context
)