You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I've identified a couple of use-cases for running Datasette in a way that over-rides the default way that internal URLs are generated.
Running behind a reverse proxy. I tried running Datasette behind a proxy and found that some of the generated internal links incorrectly referenced http://127.0.0.1:8001/fixtures/... - when they should have been referencing http://my-host.my-domain.com/fixtures/... - this is a problem both for links within the HTML interface but also for the toggle_url keys returned in the JSON as part of the facets datastructure.
I would like it to be possible to host a Datasette instance at e.g. https://www.mynewspaper.com/interactives/2018/election-results/ - either through careful HTTP proxying or, once Datasette has been ported to ASGI, by mounting a Datasette ASGI instance deep within an existing set of URL routes.
I'm going to add a url_prefix configuration option. This will default to "", which means Datasette will behave as it does at the moment - it will use / for most URL prefixes in the HTML version, and an absolute URL derived from the incoming Host header for URLs that are returned as part of the JSON output.
If url_prefix is set to another value (either a full URL or a path) then this path will be appended to all generated URLs.
Putting this here in case anyone else encounters the same issue with nginx, I was able to resolve it by passing the header in the nginx proxy config (i.e., proxy_set_header Host $host).
Hey @simonw is the url_prefix config option available in another branch, it looks like you've written some tests for it above? In 0.32 I get "url_prefix is not a valid option". I think this would be really helpful!
This would be really handy for proxying datasette in another domain's subdirectory I believe this will allow folks to run upstream authentication, but the links break if the url_prefix doesn't match.
I'd prefer not to host a proxied version of datasette on a subdomain (e.g. datasette.myurl.com b/c then I gotta worry about sharing authorization cookies with the subdomain, which I just assume not do, but...)
Edit: I see the wip-url-prefix branch, I may try with that 8da2db4
Agreed, this would be nice to have. I'm currently working around it in nginx with additional location blocks:
location /datasette/ {
proxy_pass http://127.0.0.1:8001/;
proxy_redirect off;
include proxy_params;
}
location /dna-protein-genome/ {
proxy_pass http://127.0.0.1:8001/dna-protein-genome/;
proxy_redirect off;
include proxy_params;
}
location /rna-protein-genome/ {
proxy_pass http://127.0.0.1:8001/rna-protein-genome/;
proxy_redirect off;
include proxy_params;
}
The 2nd and 3rd above are my databases. This works, but I have a small problem with URLs like /rna-protein-genome?params.... that I could fix with some more nginx munging. I seem to do this sort of thing once every 5 years and then have to look it all up again.
FWIW I did a dumb merge of the branch here: https://github.com/jsfenfen/datasette and it seemed to work in that I could run stuff at a subdirectory, but ended up abandoning it in favor of just posting a subdomain because getting the nginx configs right was making me crazy. I still would prefer posting at a subdirectory but the subdomain seems simpler at the moment.
@simonw What about allowing a base url. The <base>....</base> tag has been around forever. Then just use all relative URLs, which I guess is likely what you already do. See https://www.w3schools.com/TAGs/tag_base.asp
This would also be useful for running Datasette in Jupyter notebooks on Binder. While you can use Jupyter-server-proxy to access Datasette on Binder, the links are broken.
Why run Datasette on Binder? I'm developing a range of Jupyter notebooks that are aimed at getting humanities researchers to explore data from libraries, archives, and museums. Many of them are aimed at researchers with limited digital skills, so being able to run examples in Binder without them installing anything is fantastic.
For example, there are a series of notebooks that help researchers harvest digitised historical newspaper articles from Trove. The metadata from this harvest is saved as a CSV file that users can download. I've also provided some extra notebooks that use Pandas etc to demonstrate ways of analysing and visualising the harvested data.
But it would be really nice if, after completing a harvest, the user could spin up Datasette for some initial exploration of their harvested data without ever leaving their browser.
@simonw You're welcome - I was just trying it out back in December as I thought it should work. Now there's a pandemic to work on though.... so no time at all for more at the moment. BTW, I have datasette running on several protein and full (virus) genome databases I build, and it's great - thank you! Hi and best regards to you & Nat :-)
Hi Simon - I'm just (trying, at least) to follow along in the above. I can't try it out now, but I will if no one else gets to it. Sorry I didn't write any tests in the original bit of code I pushed - I was just trying to see if it could work & whether you'd want to maybe head in that direction. Anyway, thank you, I will certainly use this. Comment back here if no one tried it out & I'll make time.
Thanks! I'm trying to launch Datasette from within a notebook using the jupyter-server-proxy and the new base_url parameter. While the assets load ok, and the breadcrumb navigation works, the facet links don't seem to use the base_url. Or have I missed something?
This looks great, thanks for the share! I refactored it into a self-contained function, binding on a random available TCP port (multi-user context). I am using subprocess API directly since the %run magic was leaving defunct process behind :/
importsocketfromsignalimportSIGINTfromsubprocessimportPopen, PIPEfromIPython.displayimportdisplay, HTMLfromnotebook.notebookappimportlist_running_serversdefget_free_tcp_port():
""" Get a free TCP port. """tcp=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
tcp.bind(('', 0))
_, port=tcp.getsockname()
tcp.close()
returnportdefdatasette(database):
""" Run datasette on an SQLite database. """# Get current running serversservers=list_running_servers()
# Get the current base urlbase_url=next(servers)['base_url']
# Get a free portport=get_free_tcp_port()
# Create a base url for Datasette suing the proxy pathproxy_url=f'{base_url}proxy/absolute/{port}/'# Display a link to Datasettedisplay(HTML(f'<p><a href="{proxy_url}">View Datasette</a> (Click on the stop button to close the Datasette server)</p>'))
# Launch DatasettewithPopen(
[
'python', '-m', 'datasette', '--',
database,
'--port', str(port),
'--config', f'base_url:{proxy_url}'
],
stdout=PIPE,
stderr=PIPE,
bufsize=1,
universal_newlines=True
) asp:
print(p.stdout.readline(), end='')
whileTrue:
try:
line=p.stderr.readline()
ifnotline:
breakprint(line, end='')
exit_code=p.poll()
exceptKeyboardInterrupt:
p.send_signal(SIGINT)
Ideally, I'd like some extra magic to notify users when they are leaving the closing the notebook tab and make them terminate the running datasette processes. I'll be looking for it.
Activity
simonw commentedon Jan 6, 2019
I found a really nice pattern for writing the unit tests for this (though it would look even nicer with a solution to #395)
kevindkeogh commentedon Jun 6, 2019
Hey was this ever merged? Trying to run this behind nginx, and encountering this issue.
kevindkeogh commentedon Jun 7, 2019
Putting this here in case anyone else encounters the same issue with nginx, I was able to resolve it by passing the header in the nginx proxy config (i.e.,
proxy_set_header Host $host).jsfenfen commentedon Nov 21, 2019
Hey @simonw is the url_prefix config option available in another branch, it looks like you've written some tests for it above? In 0.32 I get "url_prefix is not a valid option". I think this would be really helpful!
This would be really handy for proxying datasette in another domain's subdirectory I believe this will allow folks to run upstream authentication, but the links break if the url_prefix doesn't match.
I'd prefer not to host a proxied version of datasette on a subdomain (e.g. datasette.myurl.com b/c then I gotta worry about sharing authorization cookies with the subdomain, which I just assume not do, but...)
Edit: I see the wip-url-prefix branch, I may try with that 8da2db4
terrycojones commentedon Dec 18, 2019
Agreed, this would be nice to have. I'm currently working around it in
nginxwith additional location blocks:The 2nd and 3rd above are my databases. This works, but I have a small problem with URLs like
/rna-protein-genome?params....that I could fix with some more nginx munging. I seem to do this sort of thing once every 5 years and then have to look it all up again.Thanks!
terrycojones commentedon Dec 18, 2019
Hmmm, wait, maybe my mindless (copy/paste) use of
proxy_redirectis causing me grief...jsfenfen commentedon Dec 18, 2019
FWIW I did a dumb merge of the branch here: https://github.com/jsfenfen/datasette and it seemed to work in that I could run stuff at a subdirectory, but ended up abandoning it in favor of just posting a subdomain because getting the nginx configs right was making me crazy. I still would prefer posting at a subdirectory but the subdomain seems simpler at the moment.
terrycojones commentedon Dec 18, 2019
@simonw What about allowing a base url. The
<base>....</base>tag has been around forever. Then just use all relative URLs, which I guess is likely what you already do. See https://www.w3schools.com/TAGs/tag_base.aspbetatim commentedon Mar 23, 2020
On mybinder.org we allow access to arbitrary processes listening on a port inside the container via a reverse proxy.
This means we need support for a proxy prefix as the proxy ends up running at a URL like
/something/random/proxy/datasette/...An example that shows the problem is https://github.com/psychemedia/jupyterserverproxy-datasette-demo. Launch directly into a datasette instance on mybinder.org with https://mybinder.org/v2/gh/psychemedia/jupyterserverproxy-datasette-demo/master?urlpath=datasette then try to follow links inside the UI.
wragge commentedon Mar 23, 2020
This would also be useful for running Datasette in Jupyter notebooks on Binder. While you can use Jupyter-server-proxy to access Datasette on Binder, the links are broken.
Why run Datasette on Binder? I'm developing a range of Jupyter notebooks that are aimed at getting humanities researchers to explore data from libraries, archives, and museums. Many of them are aimed at researchers with limited digital skills, so being able to run examples in Binder without them installing anything is fantastic.
For example, there are a series of notebooks that help researchers harvest digitised historical newspaper articles from Trove. The metadata from this harvest is saved as a CSV file that users can download. I've also provided some extra notebooks that use Pandas etc to demonstrate ways of analysing and visualising the harvested data.
But it would be really nice if, after completing a harvest, the user could spin up Datasette for some initial exploration of their harvested data without ever leaving their browser.
terrycojones commentedon Mar 23, 2020
I just updated #652 to remove a merge conflict. I think it's an easy way to add this functionality. I don't have time to do more though, sorry!
simonw commentedon Mar 23, 2020
Thanks very much @terrycojones - I'll see if I can finish it up from here.
terrycojones commentedon Mar 23, 2020
@simonw You're welcome - I was just trying it out back in December as I thought it should work. Now there's a pandemic to work on though.... so no time at all for more at the moment. BTW, I have datasette running on several protein and full (virus) genome databases I build, and it's great - thank you! Hi and best regards to you & Nat :-)
8 remaining items
base_url configuration setting, refs #394
simonw commentedon Mar 24, 2020
OK, I have an implementation of this over in the
base-urlbranch (see pull request #708) which is passing all of the unit tests.Anyone willing to give it a quick test and see if it works for your particular use-case? You can install it with:
Then you can run Datasette like this:
terrycojones commentedon Mar 24, 2020
Hi Simon - I'm just (trying, at least) to follow along in the above. I can't try it out now, but I will if no one else gets to it. Sorry I didn't write any tests in the original bit of code I pushed - I was just trying to see if it could work & whether you'd want to maybe head in that direction. Anyway, thank you, I will certainly use this. Comment back here if no one tried it out & I'll make time.
simonw commentedon Mar 25, 2020
I got this working as a proxied instance inside Binder, building on @psychemedia's work: simonw/jupyterserverproxy-datasette-demo#1
Now that I've seen it working there I'm going to land the pull request.
simonw commentedon Mar 25, 2020
Shipped in 0.39: https://datasette.readthedocs.io/en/latest/changelog.html#v0-39
terrycojones commentedon Mar 25, 2020
Great - thanks again.
wragge commentedon Mar 26, 2020
Thanks! I'm trying to launch Datasette from within a notebook using the jupyter-server-proxy and the new
base_urlparameter. While the assets load ok, and the breadcrumb navigation works, the facet links don't seem to use thebase_url. Or have I missed something?My test repository is here: https://github.com/wragge/datasette-test
base_url configuration setting, closes #394
base_url configuration setting, closes #394
LVerneyPEReN commentedon Jun 10, 2020
Hi,
I came across this issue while looking for a way to spawn Datasette as a SQLite files viewer in JupyterLab. I found https://github.com/simonw/jupyterserverproxy-datasette-demo which seems to be the most up to date proof of concept, but it seems to be failing to list the available db (at least in the Binder demo, https://hub.gke.mybinder.org/user/simonw-jupyters--datasette-demo-uw4dmlnn/datasette/, I only have
:memory).Does anyone tried to improve on this proof of concept to have a Datasette visualization for SQLite files?
Thanks!
wragge commentedon Jun 10, 2020
There's a working demo here: https://github.com/wragge/datasette-test
And if you want something that's more than just proof-of-concept, here's a notebook which does some harvesting from web archives and then displays the results using Datasette: https://nbviewer.jupyter.org/github/GLAM-Workbench/web-archives/blob/master/explore_presentations.ipynb
LVerneyPEReN commentedon Jun 11, 2020
Hi @wragge,
This looks great, thanks for the share! I refactored it into a self-contained function, binding on a random available TCP port (multi-user context). I am using subprocess API directly since the
%runmagic was leaving defunct process behind :/Ideally, I'd like some extra magic to notify users when they are leaving the closing the notebook tab and make them terminate the running datasette processes. I'll be looking for it.