from cartoframes.auth import set_default_credentials

set_default_credentials('creds.json')

"""
Explanation: Getting started with CARTOframes
1. Connect to a CARTO account

creds.json file sample:

{
    username: 'john-smith',
    api_key: '1a2b3c4d5e6f7g8h'
}
End of explanation
"""


from cartoframes.data.observatory import Enrichment
from cartoframes.data.services import Geocoding, Isolines
import pandas as pd

stores_df = pd.read_csv('http://libs.cartocdn.com/cartoframes/files/starbucks_brooklyn.csv')
stores_gdf, _ = Geocoding().geocode(stores_df, street='address')
aoi_gdf, _ = Isolines().isochrones(stores_gdf, [15*60], mode='walk')
aoi_enriched_gdf = Enrichment().enrich_polygons(aoi_gdf, ['total_pop_3cf008b3'])

"""
Explanation: 2. Enrich your data
End of explanation
"""


from cartoframes.viz import Map, Layer, color_continuous_style, size_continuous_style

result_map = Map([
    Layer(aoi_enriched_gdf, color_continuous_style('total_pop', stroke_width=0, opacity=0.7)),
    Layer(stores_gdf, size_continuous_style('revenue', stroke_color='white'), default_widget=True)
])

result_map

"""
Explanation: 3. Create a visualization
End of explanation
"""


result_map.publish('onboarding_analysis', password=None, if_exists='replace')

"""
Explanation: 4. Share your visualization
End of explanation
"""


