Skip to content

Commit

Permalink
Merge pull request #752 from CartoDB/744-default-api-key
Browse files Browse the repository at this point in the history
744 default api key
  • Loading branch information
Jesus89 authored Jun 10, 2019
2 parents 54cf937 + 2e8d187 commit 2e1a1aa
Show file tree
Hide file tree
Showing 20 changed files with 1,174 additions and 1,225 deletions.
1 change: 1 addition & 0 deletions NEWS.rst
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Updates
- Add support for DataFrames visualization (#735)
- Reverse Map layers order (#742)
- Infer legend prop from the type (#743)
- Use `default_public` as default api key (#744)

0.10.0
------
Expand Down
2 changes: 1 addition & 1 deletion cartoframes/assets/templates/viz/legends.html.j2
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
heading="{{layer.legend.title}}"
description="{{layer.legend.description}}"
>
{{ createLegend(layer.legend, 'layer%d_legend' % loop.revindex0) }}
{{ createLegend(layer.legend, 'layer%d_legend' % (loop.revindex0 - 1)) }}
{% if layer.legend.footer %}
<span slot="footer">{{layer.legend.footer}}</span>
{% endif %}
Expand Down
94 changes: 64 additions & 30 deletions cartoframes/auth/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,51 +6,85 @@
_default_context = None


def set_default_context(context=None, base_url=None, api_key=None, creds=None, session=None):
def set_default_context(first=None, second=None, base_url=None, api_key=None, context=None, creds=None, session=None):
"""set_default_context
From a pair base_url, api_key.
Args:
context (:py:class:`Context <cartoframes.auth.Context>`):
A :py:class:`Context <cartoframes.auth.Context>` instance.
base_url (str): Base URL of CARTO user account. Cloud-based accounts
should use the form ``https://{username}.carto.com`` (e.g.,
https://eschbacher.carto.com for user ``eschbacher``) whether on
a personal or multi-user account. On-premises installation users
should ask their admin.
api_key (str): CARTO API key.
creds (:py:class:`Credentials <cartoframes.credentials.Credentials>`):
A :py:class:`Credentials <cartoframes.credentials.Credentials>`
instance can be used in place of a `base_url`/`api_key` combination.
session (requests.Session, optional): requests session. See `requests
documentation
<http://docs.python-requests.org/en/master/user/advanced/>`__
for more information.
.. code::
Example:
from cartoframes.auth import set_default_context
From a pair base_url, api_key.
set_default_context(
base_url='https://your_user_name.carto.com',
api_key='your api key'
)
.. code::
From context.
from cartoframes.auth import set_default_context
.. code::
set_default_context(
base_url='https://your_user_name.carto.com',
api_key='your api key'
)
from cartoframes.auth import Context, set_default_context
From a base_url (for public datasets).
The API key `default_public` is used by default.
context = Context(
base_url='https://your_user_name.carto.com',
api_key='your api key'
)
set_default_context(context)
.. code::
From credentials.
from cartoframes.auth import set_default_context
.. code::
set_default_context('https://your_user_name.carto.com')
from cartoframes.auth import Credentials, set_default_context
From a context.
creds = Credentials(
key='abcdefg',
username='cartoframes'
)
set_default_context(creds)
.. code::
from cartoframes.auth import Context, set_default_context
context = Context(
base_url='https://your_user_name.carto.com',
api_key='your api key'
)
set_default_context(context)
From credentials.
.. code::
from cartoframes.auth import Credentials, set_default_context
creds = Credentials(
key='abcdefg',
username='cartoframes'
)
set_default_context(creds)
"""
global _default_context
if isinstance(context, Context):
_default_context = context
elif isinstance(base_url, str) and isinstance(api_key, str):
_default_context = Context(base_url=base_url, api_key=api_key, session=session)
elif isinstance(creds, Credentials):
_default_context = Context(creds=creds, session=session)

_context = context if first is None else first
_base_url = base_url if first is None else first
_api_key = (api_key if second is None else second) or 'default_public'
_creds = creds if first is None else first

if isinstance(_context, Context):
_default_context = _context
elif isinstance(_base_url, str) and isinstance(_api_key, str):
_default_context = Context(base_url=_base_url, api_key=_api_key, session=session)
elif isinstance(_creds, Credentials):
_default_context = Context(creds=_creds, session=session)
else:
raise ValueError('Invalid inputs. Pass a Context, a base_url and api_key pair, or a Credentials object.')

Expand Down
2 changes: 1 addition & 1 deletion cartoframes/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ class CartoContext(object):
"""

def __init__(self, base_url=None, api_key=None, creds=None, session=None,
def __init__(self, base_url=None, api_key='default_public', creds=None, session=None,
verbose=0):

self.creds = Credentials(creds=creds, key=api_key, base_url=base_url)
Expand Down
4 changes: 2 additions & 2 deletions cartoframes/viz/layer.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ class Layer(object):
from cartoframes.viz import Layer
set_default_context(
base_url='https://cartovl.carto.com/',
api_key='default_public'
base_url='https://your_user_name.carto.com',
api_key='your api key'
)
Layer(
Expand Down
4 changes: 2 additions & 2 deletions cartoframes/viz/map.py
Original file line number Diff line number Diff line change
Expand Up @@ -437,15 +437,15 @@ def __init__(self, template_path='viz/basic.html.j2'):
def set_content(
self, size, layers, bounds, viewport=None, basemap=None,
default_legend=None, show_info=None,
_carto_vl_path=None, _airship_path=None, title=None):
_carto_vl_path=None, _airship_path=None, title='CARTOframes'):

self.html = self._parse_html_content(
size, layers, bounds, viewport, basemap, default_legend, show_info,
_carto_vl_path, _airship_path, title)

def _parse_html_content(
self, size, layers, bounds, viewport, basemap=None, default_legend=None,
show_info=None, _carto_vl_path=None, _airship_path=None, title='CARTO VL + CARTOframes'):
show_info=None, _carto_vl_path=None, _airship_path=None, title=None):

token = ''
basecolor = ''
Expand Down
65 changes: 25 additions & 40 deletions examples/debug/API/basemaps.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,7 @@
"from cartoframes.auth import set_default_context\n",
"from cartoframes.viz import Map, Layer, basemaps\n",
"\n",
"set_default_context(\n",
" base_url='https://cartovl.carto.com/', \n",
" api_key='default_public'\n",
")"
"set_default_context('https://cartovl.carto.com/')"
]
},
{
Expand All @@ -31,10 +28,10 @@
" height: 632px;\n",
" \"\n",
" srcDoc=\"\n",
" <!DOCTYPE html>\n",
" <!DOCTYPE html>\n",
" <html lang=&quot;en&quot;>\n",
" <head>\n",
" <title>CARTO VL + CARTOframes</title>\n",
" <title>CARTOframes</title>\n",
" <meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot;>\n",
" <meta charset=&quot;UTF-8&quot;>\n",
" <!-- Include CARTO VL JS -->\n",
Expand Down Expand Up @@ -143,10 +140,6 @@
" font-family: 'Open Sans';\n",
" }\n",
"\n",
" as-legends {\n",
" --as-legends--padding: 16px;\n",
" }\n",
"\n",
" as-widget-header .as-widget-header__header {\n",
" margin-bottom: 8px;\n",
" }\n",
Expand Down Expand Up @@ -693,7 +686,7 @@
" updatePopup(tempPopup, event, attrs)\n",
" });\n",
" }\n",
" \n",
"\n",
" function setPopupsHover(tempPopup, interactivity, attrs) {\n",
" interactivity.on('featureHover', (event) => {\n",
" updatePopup(tempPopup, event, attrs)\n",
Expand All @@ -705,21 +698,21 @@
" const feature = event.features[0];\n",
"\n",
" let popupHTML = '';\n",
" \n",
"\n",
" attrs.forEach((item) => {\n",
" let value = feature.variables[item.name].value;\n",
" value = formatValue(value)\n",
" \n",
"\n",
" popupHTML += `\n",
" <span class=&quot;popup-name&quot;>${item.title}</span>\n",
" <span class=&quot;popup-value&quot;>${value}</span>\n",
" `;\n",
" });\n",
" \n",
"\n",
" popup\n",
" .setLngLat([event.coordinates.lng, event.coordinates.lat])\n",
" .setHTML(`<div class=&quot;popup-content&quot;>${popupHTML}</div>`);\n",
" \n",
"\n",
" if (!popup.isOpen()) {\n",
" popup.addTo(map);\n",
" }\n",
Expand Down Expand Up @@ -797,7 +790,7 @@
"</iframe>"
],
"text/plain": [
"<cartoframes.viz.map.Map at 0x7f85a41c5898>"
"<cartoframes.viz.map.Map at 0x7f8329026b70>"
]
},
"execution_count": 2,
Expand Down Expand Up @@ -825,10 +818,10 @@
" height: 632px;\n",
" \"\n",
" srcDoc=\"\n",
" <!DOCTYPE html>\n",
" <!DOCTYPE html>\n",
" <html lang=&quot;en&quot;>\n",
" <head>\n",
" <title>CARTO VL + CARTOframes</title>\n",
" <title>CARTOframes</title>\n",
" <meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot;>\n",
" <meta charset=&quot;UTF-8&quot;>\n",
" <!-- Include CARTO VL JS -->\n",
Expand Down Expand Up @@ -937,10 +930,6 @@
" font-family: 'Open Sans';\n",
" }\n",
"\n",
" as-legends {\n",
" --as-legends--padding: 16px;\n",
" }\n",
"\n",
" as-widget-header .as-widget-header__header {\n",
" margin-bottom: 8px;\n",
" }\n",
Expand Down Expand Up @@ -1487,7 +1476,7 @@
" updatePopup(tempPopup, event, attrs)\n",
" });\n",
" }\n",
" \n",
"\n",
" function setPopupsHover(tempPopup, interactivity, attrs) {\n",
" interactivity.on('featureHover', (event) => {\n",
" updatePopup(tempPopup, event, attrs)\n",
Expand All @@ -1499,21 +1488,21 @@
" const feature = event.features[0];\n",
"\n",
" let popupHTML = '';\n",
" \n",
"\n",
" attrs.forEach((item) => {\n",
" let value = feature.variables[item.name].value;\n",
" value = formatValue(value)\n",
" \n",
"\n",
" popupHTML += `\n",
" <span class=&quot;popup-name&quot;>${item.title}</span>\n",
" <span class=&quot;popup-value&quot;>${value}</span>\n",
" `;\n",
" });\n",
" \n",
"\n",
" popup\n",
" .setLngLat([event.coordinates.lng, event.coordinates.lat])\n",
" .setHTML(`<div class=&quot;popup-content&quot;>${popupHTML}</div>`);\n",
" \n",
"\n",
" if (!popup.isOpen()) {\n",
" popup.addTo(map);\n",
" }\n",
Expand Down Expand Up @@ -1591,7 +1580,7 @@
"</iframe>"
],
"text/plain": [
"<cartoframes.viz.map.Map at 0x7f85a41c57f0>"
"<cartoframes.viz.map.Map at 0x7f8364761390>"
]
},
"execution_count": 3,
Expand Down Expand Up @@ -1619,10 +1608,10 @@
" height: 632px;\n",
" \"\n",
" srcDoc=\"\n",
" <!DOCTYPE html>\n",
" <!DOCTYPE html>\n",
" <html lang=&quot;en&quot;>\n",
" <head>\n",
" <title>CARTO VL + CARTOframes</title>\n",
" <title>CARTOframes</title>\n",
" <meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot;>\n",
" <meta charset=&quot;UTF-8&quot;>\n",
" <!-- Include CARTO VL JS -->\n",
Expand Down Expand Up @@ -1731,10 +1720,6 @@
" font-family: 'Open Sans';\n",
" }\n",
"\n",
" as-legends {\n",
" --as-legends--padding: 16px;\n",
" }\n",
"\n",
" as-widget-header .as-widget-header__header {\n",
" margin-bottom: 8px;\n",
" }\n",
Expand Down Expand Up @@ -2281,7 +2266,7 @@
" updatePopup(tempPopup, event, attrs)\n",
" });\n",
" }\n",
" \n",
"\n",
" function setPopupsHover(tempPopup, interactivity, attrs) {\n",
" interactivity.on('featureHover', (event) => {\n",
" updatePopup(tempPopup, event, attrs)\n",
Expand All @@ -2293,21 +2278,21 @@
" const feature = event.features[0];\n",
"\n",
" let popupHTML = '';\n",
" \n",
"\n",
" attrs.forEach((item) => {\n",
" let value = feature.variables[item.name].value;\n",
" value = formatValue(value)\n",
" \n",
"\n",
" popupHTML += `\n",
" <span class=&quot;popup-name&quot;>${item.title}</span>\n",
" <span class=&quot;popup-value&quot;>${value}</span>\n",
" `;\n",
" });\n",
" \n",
"\n",
" popup\n",
" .setLngLat([event.coordinates.lng, event.coordinates.lat])\n",
" .setHTML(`<div class=&quot;popup-content&quot;>${popupHTML}</div>`);\n",
" \n",
"\n",
" if (!popup.isOpen()) {\n",
" popup.addTo(map);\n",
" }\n",
Expand Down Expand Up @@ -2385,7 +2370,7 @@
"</iframe>"
],
"text/plain": [
"<cartoframes.viz.map.Map at 0x7f85e0050160>"
"<cartoframes.viz.map.Map at 0x7f8329026b38>"
]
},
"execution_count": 4,
Expand Down
Loading

0 comments on commit 2e1a1aa

Please sign in to comment.