feat: optional kepler.gl integration (#12)

This commit is contained in:
Felix Spöttel
2022-07-04 16:22:27 +02:00
committed by GitHub
parent d252c6b8f3
commit 6a5cb3c3c3
4 changed files with 31 additions and 7 deletions

View File

@@ -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
@@ -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

View File

@@ -72,12 +72,22 @@ def main(distance, size, output, filename, algorithm, open, debug):
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")
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")

View File

@@ -1,5 +1,3 @@
import math
from keplergl import KeplerGl
from pathlib import Path
from pkg_resources import resource_filename
import json
@@ -8,6 +6,14 @@ import numpy as np
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."""
@@ -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()

View File

@@ -28,13 +28,13 @@ setup(
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,