From e6c0fec0744b5c7ffd59792219e9b02fa9510bb1 Mon Sep 17 00:00:00 2001 From: Simon Willison Date: Mon, 25 Jun 2018 20:06:28 -0700 Subject: [PATCH] Packaged everything up as a Datasette plugin I used a variant on the pattern described here to run "npm run build" when creating the wheel: http://cra.mr/2014/12/19/distributing-js-with-python To generate just the static assets (in datasette_vega/static) run this: python3 setup.py build_static To build the package (and automatically run the above) do this: python3 setup.py bdist_wheel --- .gitignore | 2 ++ datasette_vega/__init__.py | 11 +++++++ setup.py | 66 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 79 insertions(+) create mode 100644 datasette_vega/__init__.py create mode 100644 setup.py diff --git a/.gitignore b/.gitignore index 0238662..45d40dd 100644 --- a/.gitignore +++ b/.gitignore @@ -22,3 +22,5 @@ dist npm-debug.log* yarn-debug.log* yarn-error.log* + +static diff --git a/datasette_vega/__init__.py b/datasette_vega/__init__.py new file mode 100644 index 0000000..ea03f63 --- /dev/null +++ b/datasette_vega/__init__.py @@ -0,0 +1,11 @@ +from datasette import hookimpl + + +@hookimpl +def extra_css_urls(): + return ["/-/static-plugins/datasette_vega/datasette-vega.css"] + + +@hookimpl +def extra_js_urls(): + return ["/-/static-plugins/datasette_vega/datasette-vega.js"] diff --git a/setup.py b/setup.py new file mode 100644 index 0000000..1d573b4 --- /dev/null +++ b/setup.py @@ -0,0 +1,66 @@ +from distutils.core import Command +from setuptools import setup +from subprocess import check_output +from wheel.bdist_wheel import bdist_wheel +import os + +VERSION = '0.1' +ROOT = os.path.dirname(os.path.abspath(__file__)) + + +def get_long_description(): + with open(os.path.join( + os.path.dirname(os.path.abspath(__file__)), 'README.md' + ), encoding='utf8') as fp: + return fp.read() + + +class BdistWheelWithBuildStatic(bdist_wheel): + def run(self): + self.run_command('build_static') + return bdist_wheel.run(self) + + +class BuildStatic(Command): + user_options = [] + + def initialize_options(self): + pass + + def finalize_options(self): + pass + + def run(self): + check_output(['npm', 'run', 'build'], cwd=ROOT) + check_output(['mkdir', '-p', 'datasette_vega/static'], cwd=ROOT) + check_output("mv build/static/js/*.js datasette_vega/static/datasette-vega.js", shell=True, cwd=ROOT) + check_output("mv build/static/css/*.css datasette_vega/static/datasette-vega.css", shell=True, cwd=ROOT) + + +setup( + name='datasette-vega', + description='A Datasette plugin that provides tools for generating charts using Vega', + long_description=get_long_description(), + long_description_content_type='text/markdown', + author='Simon Willison', + url='https://github.com/simonw/datasette-vega', + license='Apache License, Version 2.0', + version=VERSION, + packages=['datasette_vega'], + entry_points={ + 'datasette': [ + 'cluster_map = datasette_vega' + ], + }, + package_data={ + 'datasette_vega': [ + 'static/datasette-vega.js', + 'static/datasette-vega.css', + ], + }, + cmdclass={ + 'bdist_wheel': BdistWheelWithBuildStatic, + 'build_static': BuildStatic, + }, + install_requires=['datasette'] +)