Skip to content

Commit

Permalink
adds join_url for more functionalized url composition
Browse files Browse the repository at this point in the history
  • Loading branch information
andy-esch committed Oct 11, 2017
1 parent 96a44c1 commit 1737675
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 8 deletions.
8 changes: 4 additions & 4 deletions cartoframes/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from carto.exceptions import CartoException

from .credentials import Credentials
from .utils import dict_items, normalize_colnames, norm_colname
from .utils import dict_items, normalize_colnames, norm_colname, join_url
from .layer import BaseMap
from .maps import non_basemap_layers, get_map_name, get_map_template

Expand Down Expand Up @@ -217,7 +217,7 @@ def write(self, df, table_name, temp_dir='/tmp', overwrite=False,
'minutes.\n'
'\033[1mNote:\033[0m `CartoContext.map` will not work on '
'this table until its geometries are created.'.format(
table_url='/'.join((self.creds.base_url(),
table_url=join_url((self.creds.base_url(),
'dataset',
final_table_name, )),
job_id=status.get('job_id'),
Expand All @@ -227,7 +227,7 @@ def write(self, df, table_name, temp_dir='/tmp', overwrite=False,
self.sql_client.send(query)

tqdm.write('Table successfully written to CARTO: {table_url}'.format(
table_url='/'.join((self.creds.base_url(),
table_url=join_url((self.creds.base_url(),
'dataset',
final_table_name, ))))

Expand Down Expand Up @@ -734,7 +734,7 @@ def map(self, layers=None, interactive=True,
options.update(self._get_bounds(nb_layers))

map_name = self._send_map_template(layers, has_zoom=has_zoom)
api_url = '/'.join((self.creds.base_url(), 'api/v1/map', ))
api_url = join_url((self.creds.base_url(), 'api/v1/map', ))

static_url = ('{api_url}/static/named/{map_name}'
'/{width}/{height}.png?{params}').format(
Expand Down
7 changes: 3 additions & 4 deletions cartoframes/layer.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import pandas as pd
import webcolors

from cartoframes.utils import cssify
from cartoframes.utils import cssify, join_url
from cartoframes.styling import BinMethod, mint, antique, get_scheme_cartocss

# colors map data layers without color specified
Expand Down Expand Up @@ -73,8 +73,7 @@ def __init__(self, source='voyager', labels='back', only_labels=False):
label_type = '_labels_under'
style = source + (label_type if labels == 'back'
else '_nolabels')
self.url = '/'.join(s.strip('/') for s in
(stem,
self.url = join_url((stem,
'{style}/{{z}}/{{x}}/{{y}}.png'.format(
style=style)
))
Expand All @@ -86,7 +85,7 @@ def __init__(self, source='voyager', labels='back', only_labels=False):
# self.url = source
else:
raise ValueError("`source` must be one of 'dark', 'light', or "
"voyager")
"'voyager'")

def is_basic(self):
"""Does BaseMap pull from CARTO default basemaps?
Expand Down
5 changes: 5 additions & 0 deletions cartoframes/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,8 @@ def norm_colname(colname):
if final_name[0].isdigit():
return '_' + final_name
return final_name


def join_url(parts):
"""join parts of URL into complete url"""
return '/'.join(s.strip('/') for s in parts)

0 comments on commit 1737675

Please sign in to comment.