Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ability for plugins to define extra JavaScript and CSS #214

Closed
simonw opened this issue Apr 16, 2018 · 6 comments
Closed

Ability for plugins to define extra JavaScript and CSS #214

simonw opened this issue Apr 16, 2018 · 6 comments

Comments

@simonw
Copy link
Owner

simonw commented Apr 16, 2018

This can hook in to the existing extra_css_urls and extra_js_urls mechanism:

datasette/datasette/app.py

Lines 304 to 305 in b2955d9

'extra_css_urls': self.ds.extra_css_urls(),
'extra_js_urls': self.ds.extra_js_urls(),

The plugins should be able to bundle their own assets though, so it will also have to integrate with the /static/ static mounts mechanism somehow:

datasette/datasette/app.py

Lines 1255 to 1257 in b2955d9

app.static('/-/static/', str(app_root / 'datasette' / 'static'))
for path, dirname in self.static_mounts:
app.static(path, dirname)

Refs #14

@simonw simonw added the plugins label Apr 16, 2018
@simonw
Copy link
Owner Author

simonw commented Apr 16, 2018

Packaging JS and CSS in a pip installable wheel is fiddly but possible. http://peak.telecommunity.com/DevCenter/PythonEggs#accessing-package-resources

from pkg_resources import resource_string
foo_config = resource_string(__name__, 'foo.conf')

@simonw
Copy link
Owner Author

simonw commented Apr 16, 2018

This looks like a good example: funkey/nyroglancer@d4438ab

@simonw
Copy link
Owner Author

simonw commented Apr 16, 2018

resource_stream returns a file-like object which may be better for serving from Sanic.

@simonw
Copy link
Owner Author

simonw commented Apr 17, 2018

I figured out the recipe for bundling static assets in a plugin: simonw/datasette-plugin-demos@26c5548 (and then python3 setup.py bdist_wheel)

Having done that, I ran pip install ../datasette-plugin-demos/dist/datasette_plugin_demos-0.2-py3-none-any.whl from my Datasette virtual environment and then did the following:

>>> import pkg_resources
>>> pkg_resources.resource_stream(
...    'datasette_plugin_demos', 'static/plugin.js'
... ).read()
b"alert('hello');\n"
>>> pkg_resources.resource_filename(
...    'datasette_plugin_demos', 'static/plugin.js'
... )
'..../venv/lib/python3.6/site-packages/datasette_plugin_demos/static/plugin.js'
>>> pkg_resources.resource_string(
...    'datasette_plugin_demos', 'static/plugin.js'
... )
b"alert('hello');\n"

@simonw
Copy link
Owner Author

simonw commented Apr 17, 2018

One possible option: let plugins bundle their own static/ directory and then register themselves with Datasette, then have /-/static-plugins/name-of-plugin/... serve files from that directory.

@simonw
Copy link
Owner Author

simonw commented Apr 17, 2018

Even if we automatically serve ALL static/ content from installed plugins, we'll still need them to register which files need to be linked to from extra_css_urls and extra_js_urls

simonw added a commit to simonw/datasette-plugin-demos that referenced this issue Apr 18, 2018
@simonw simonw closed this as completed in 1c36d07 Apr 18, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant