diff --git a/AUTHORS.rst b/AUTHORS.rst index a5bab2f..b213c31 100644 --- a/AUTHORS.rst +++ b/AUTHORS.rst @@ -10,4 +10,4 @@ Development Lead Contributors ------------ -None yet. Why not be the first? +* Sandeep Pandey diff --git a/CHANGELOG.md b/CHANGELOG.md index 16ba4d9..2fef950 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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__` diff --git a/README.md b/README.md index 1e078ff..dadc506 100644 --- a/README.md +++ b/README.md @@ -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**: @@ -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) @@ -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) ``` - `data` (either `None`, a single data object, or a list of data objects): @@ -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()** diff --git a/keplergl_cli/keplergl_cli.py b/keplergl_cli/keplergl_cli.py index 8e2e58f..f6c759f 100644 --- a/keplergl_cli/keplergl_cli.py +++ b/keplergl_cli/keplergl_cli.py @@ -40,7 +40,10 @@ def __init__( names=None, read_only=False, api_key=None, - style=None): + style=None, + config_file=None, + output_map=None, + open_browser=False): """Visualize data using kepler.gl Args: @@ -48,6 +51,10 @@ def __init__( 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. """ super(Visualize, self).__init__() @@ -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' + 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: text = f.read() text = text.replace('{MAPBOX_API_KEY}', self.MAPBOX_API_KEY) @@ -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 """ - # 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 \ No newline at end of file diff --git a/keplergl_cli/keplergl_config.json b/keplergl_cli/keplergl_config.json index 987328d..9d9697d 100644 --- a/keplergl_cli/keplergl_config.json +++ b/keplergl_cli/keplergl_config.json @@ -104,4 +104,4 @@ "app": "kepler.gl", "created_at": "Tue Oct 29 2019 17:15:02 GMT+0100 (Central European Standard Time)" } -} +} \ No newline at end of file diff --git a/setup.py b/setup.py index 255597e..fd839b0 100644 --- a/setup.py +++ b/setup.py @@ -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', zip_safe=False, )