Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

744 default api key #752

Merged
merged 9 commits into from
Jun 10, 2019
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
14 changes: 11 additions & 3 deletions cartoframes/auth/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
_default_context = None


def set_default_context(context=None, base_url=None, api_key=None, creds=None, session=None):
def set_default_context(context=None, base_url=None, api_key='default_public', creds=None, session=None):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm missing the Args section of the docs here. Is that going to happen package-wide in another PR?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I'll add it in this PR

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It feels a little strange to me that context can be used for a base_url or a cartoframes.Context object, but it also makes some logical sense that a base url is essentially a context if there's a default api key.

Some ideas to get around this:

  • force keyword args using the * notation in the function signature, and let the first arg be a more generic thing
  • rename the context kwarg?
  • do nothing because it's fine :) But clear docs explaining what's going on

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. The first param can be a context or a base_url. Renaming it to be more generic is a good approach to me.

"""set_default_context

From a pair base_url, api_key.
Expand All @@ -20,6 +20,14 @@ def set_default_context(context=None, base_url=None, api_key=None, creds=None, s
api_key='your api key'
)

From a base_url (for public datasets)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like the idea of educating the user about what's happening. How about saying something about how default_public is used for the API key in this case?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍


.. code::

from cartoframes.auth import set_default_context

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

From context.

.. code::
Expand Down Expand Up @@ -47,8 +55,8 @@ def set_default_context(context=None, base_url=None, api_key=None, creds=None, s
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(base_url or context, str) and isinstance(api_key, str):
_default_context = Context(base_url=(base_url or context), api_key=api_key, session=session)
elif isinstance(creds, Credentials):
_default_context = Context(creds=creds, session=session)
else:
Expand Down
2 changes: 1 addition & 1 deletion cartoframes/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,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 @@ -39,8 +39,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 @@ -436,15 +436,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