Skip to content
Open
2 changes: 1 addition & 1 deletion AUTHORS.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ Development Lead
Contributors
------------

None yet. Why not be the first?
* Sandeep Pandey <spandey.ike@gmail.com>
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Changelog

## 0.3.4 (2022-09-01)

- Arguments to save maps at user-defined location and use a custom configuration file
- Respect `open_browser=False` even during the class instantiation with data

## 0.3.3 (2022-06-27)

- Revert usage of `__geo_interface__`
Expand Down
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,7 @@ Visualize(data)
## Install

**Mapbox API key**: in order to display Mapbox-hosted maps, you need to provide
a Mapbox API key. Go to [Mapbox.com](https://account.mapbox.com/access-tokens)
to get an API key.
a Mapbox API key. Go to [Mapbox.com](https://account.mapbox.com/access-tokens) to get an API key.

**Package install**:

Expand Down Expand Up @@ -115,7 +114,7 @@ More detail over the objects in your map:

```py
from keplergl_cli import Visualize
vis = Visualize(api_key=MAPBOX_API_KEY)
vis = Visualize(api_key=MAPBOX_API_KEY, output_map = PATH_TO_SAVE_HTML_MAP, config_file = PATH_TO_JSON_CONFIG_FILE)
vis.add_data(data=data, names='name of layer')
vis.add_data(data=data2, names='name of layer')
html_path = vis.render(open_browser=True, read_only=False)
Expand All @@ -124,7 +123,7 @@ html_path = vis.render(open_browser=True, read_only=False)
**Visualize**

```py
Visualize(data=None, names=None, read_only=False, api_key=None, style=None)
Visualize(data=None, names=None, read_only=False, api_key=None, style=None, config_file=None, output_map=None)
Copy link
Owner

Choose a reason for hiding this comment

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

See below for discussion of whether open_browser should exist as a param to __init__, but if it does exist then it should be documented here

```

- `data` (either `None`, a single data object, or a list of data objects):
Expand Down Expand Up @@ -170,6 +169,8 @@ Visualize(data=None, names=None, read_only=False, api_key=None, style=None)
starts with `mapbox://`. Otherwise, a custom style using third-party map
tiles should be a URL to a JSON file that conforms to the [Mapbox Style
Specification](https://docs.mapbox.com/mapbox-gl-js/style-spec/).
- `config_file` (`string`): Path to custom JSON configuration file for kepler.
- `output_map` (`string`): If path is provided then map is saved to this path.

**Visualize.add_data()**

Expand Down
37 changes: 23 additions & 14 deletions keplergl_cli/keplergl_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,21 @@ def __init__(
names=None,
read_only=False,
api_key=None,
style=None):
style=None,
Copy link
Owner

Choose a reason for hiding this comment

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

8 params is too many to allow to be positional imo. I'd rather not change the existing API but we should add * before config_file to make the new params keyword-only

config_file=None,
Copy link
Owner

Choose a reason for hiding this comment

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

IMO this should accept a config as a python dict, not as a path to a file

output_map=None,
open_browser=False):
Copy link
Owner

Choose a reason for hiding this comment

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

I originally didn't intend open_browser to be exposed through __init__. The rationale being that passing data into here is just a shortcut for using render directly, and we don't need to expose all the same params

"""Visualize data using kepler.gl

Args:
data Optional[Union[List[]]]:
either None, a List of data objects, or a single data object. If
data is not None, then Visualize(data) will perform all steps,
including rendering and opening a browser.
`config_file` provides the path of config file.
`output_map` provides the location html file, if none then will
be dumped to temporaty files.
`open_browser` enables the browser opening if data is provided.
Comment on lines +54 to +57
Copy link
Owner

Choose a reason for hiding this comment

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

These params should be in the same format as data above

"""
super(Visualize, self).__init__()

Expand All @@ -59,23 +66,29 @@ def __init__(
msg += 'environment variable not set.\nMap may not display.'
if self.MAPBOX_API_KEY is None:
print(msg)

if config_file is None:
self.config_file = resource_filename('keplergl_cli', 'keplergl_config.json')
else:
self.config_file = config_file
if output_map is not None:
self.path = output_map+'_vis.html'
Copy link
Owner

Choose a reason for hiding this comment

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

If the user passes in a specific path, we shouldn't change it

else:
self.path = os.path.join(tempfile.mkdtemp(), 'defaultmap_vis.html')
config = self.config(style=style)
self.map = KeplerGl(config=config)

if data is not None:
self.add_data(data=data, names=names)
self.html_path = self.render(read_only=read_only)
self.html_path = self.render(read_only=read_only,open_browser=open_browser)

def config(self, style=None):
"""Load kepler.gl config and insert Mapbox API Key"""

config_file = resource_filename(
'keplergl_cli', 'keplergl_config.json')
# config_file = resource_filename('keplergl_cli', 'keplergl_config.json')

# First load config file as string, replace {MAPBOX_API_KEY} with the
# actual api key, then parse as JSON
with open(config_file) as f:
with open(self.config_file) as f:
Copy link
Owner

Choose a reason for hiding this comment

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

This function shouldn't touch a user-provided config file.

text = f.read()

text = text.replace('{MAPBOX_API_KEY}', self.MAPBOX_API_KEY)
Expand Down Expand Up @@ -143,14 +156,10 @@ def add_data(self, data, names=None):
self.map.add_data(data=datum, name=name)

def render(self, open_browser=True, read_only=False, center_map=True):
"""Export kepler.gl map to HTML file and open in Chrome
"""Export kepler.gl map to HTML file and open in defauly system browser
Copy link
Owner

Choose a reason for hiding this comment

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

Suggested change
"""Export kepler.gl map to HTML file and open in defauly system browser
"""Export kepler.gl map to HTML file and open in default system browser

"""
# Generate path to a temporary file
path = os.path.join(tempfile.mkdtemp(), 'vis.html')
self.map.save_to_html(file_name=path, read_only=read_only, center_map=center_map)

self.map.save_to_html(file_name=self.path, read_only=read_only, center_map=center_map)
# Open saved HTML file in new tab in default browser
if open_browser:
webbrowser.open_new_tab('file://' + path)

return path
webbrowser.open_new_tab('file://' + self.path)
return self.path
Copy link
Owner

Choose a reason for hiding this comment

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

Nit: here and below are missing a newline at the end of the file

2 changes: 1 addition & 1 deletion keplergl_cli/keplergl_config.json
Original file line number Diff line number Diff line change
Expand Up @@ -104,4 +104,4 @@
"app": "kepler.gl",
"created_at": "Tue Oct 29 2019 17:15:02 GMT+0100 (Central European Standard Time)"
}
}
}
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,6 @@
test_suite='tests',
tests_require=test_requirements,
url='https://github.com/kylebarron/keplergl_cli',
version='0.3.3',
version='0.3.4',
Copy link
Owner

Choose a reason for hiding this comment

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

You shouldn't change the version here; we'll have a separate PR to bump the version, which will probably be 0.4.0

zip_safe=False,
)