Skip to content

Commit

Permalink
feat: optional kepler.gl integration (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
fspoettel authored Jul 4, 2022
1 parent d252c6b commit 6a5cb3c
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 7 deletions.
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,14 @@ A cluster is created when a certain number of points (defined with `--size`) eac
Install with pip:

```sh
# with kepler.gl visualization support
pip install geoclustering[full]

# only text-based output
pip install geoclustering
```

If the install fails, you might need to install kepler.gl build dependencies:
If the `full` install fails, you might need to install kepler.gl build dependencies:

```sh
# macos
Expand Down Expand Up @@ -138,7 +142,7 @@ It is assumed that you are using **Python3.9+**. It is encouraged to [setup a vi

```sh
# install dependencies & dev-dependencies
pip install -e .[dev]
pip install -e .[dev,full]

# install a git hook that runs the code formatter before each commit.
pre-commit install
Expand Down
14 changes: 12 additions & 2 deletions geoclustering/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,22 @@ def print_debug(s):
io.write_output_file(output, "result.txt", encoded["string"])
io.write_output_file(output, "result.json", encoded["json"])
io.write_output_file(output, "result.geojson", encoded["geojson"])

vis = io.write_visualization(output, "result.html", encoded["geojson"])
if vis is None:
print_debug(f"Skipped generating visualization: kepler is not installed.")

click.echo(f"Output files saved to {Path(output).absolute()}")

if open:
print_debug(f"Opening visualization in default browser")
webbrowser.open_new_tab("file://" + str(vis.absolute()))
if vis:
webbrowser.open_new_tab("file://" + str(vis.absolute()))
print_debug(f"Opened visualization in default browser.")
else:
click.secho(
"Can't open kepler.gl: package not installed. Please re-install geoclustering with `pip install geoclustering[full]`.",
fg="yellow",
)

click.secho("Clustering completed.", fg="green")

Expand Down
14 changes: 12 additions & 2 deletions geoclustering/io.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import math
from keplergl import KeplerGl
from pathlib import Path
from pkg_resources import resource_filename
import json
Expand All @@ -8,6 +6,14 @@
import os
import sys

# kepler is optional, check if installed.
try:
from keplergl import KeplerGl
except:
has_kepler = False
else:
has_kepler = True


class HiddenPrints:
"""Disables stdout prints for a block of code."""
Expand Down Expand Up @@ -87,6 +93,10 @@ def write_output_file(dirname, filename, data):

def write_visualization(dirname, filename, data):
"""Write a visualization, ensuring parent directories."""

if not has_kepler:
return None

# Hide kepler stdout output.
with HiddenPrints():
map = KeplerGl()
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@
install_requires=[
"click",
"geojson",
"keplergl",
"numpy",
"pandas",
"scikit-learn",
],
extras_require={
"dev": ["black", "wheel", "pre-commit", "pytest"],
"full": ["keplergl"],
},
include_package_data=True,
zip_safe=False,
Expand Down

0 comments on commit 6a5cb3c

Please sign in to comment.