diff --git a/README.md b/README.md index f62f55d..a9b05d4 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/geoclustering/__main__.py b/geoclustering/__main__.py index 723c841..a4c7367 100644 --- a/geoclustering/__main__.py +++ b/geoclustering/__main__.py @@ -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") diff --git a/geoclustering/io.py b/geoclustering/io.py index 51ad016..4efe44b 100644 --- a/geoclustering/io.py +++ b/geoclustering/io.py @@ -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 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() diff --git a/setup.py b/setup.py index 5c363ab..cd55ff0 100644 --- a/setup.py +++ b/setup.py @@ -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,