Feature request: initialize input cells from url query params

I think it would be cool if we could initialize input cells from query params.

for example: https://beta.observablehq.com/@mbostock/yarn-lock-visualizer?input=<test_lib_yarn_lock_file>

would just run @mbostock’s yarn-lock-visualizer for the lib you pass

2 Likes

There is a workaround

https://beta.observablehq.com/@bumbeishvili/initialize-notebook-from-query-string-params?input=https://raw.githubusercontent.com/d3/d3/master/yarn.lock
2 Likes

super! Thanks @bumbeishvili for putting that notebook together.

I’ve been wondering how we could easily override our input cells for links sharing.

this does it! would still like to have @system do it though by default just matching cells like urls to url query params on notebook load

:hugs:

1 Like

We’ve fixed the <base href> of the page to include the query string, so here’s the shortest snippet I know of to parse the query string (using the convenient URLSearchParams):

params = new URLSearchParams(html`<a href>`.search)

The nice thing about this approach is that it’ll continue to work if you download and embed your notebook on another site, and it makes very few assumptions about the page.

Thanks for the suggestion @RandomFractals and a solution @bumbeishvili!

3 Likes

I like it! very elegant solution.

as always, much thanks!

For an array of values it seems one can pass ?c[]=1&c[]=2 in the query string and retrieve with params.getAll("c[]").

1 Like

Here is how I’ve managed to put parameters in the URL : How-to : Passing arguments in the url of the notebook / PAC / Observable.

No need for the awkward brackets (that’s a PHP thing). Just do ?c=1&c=2 and

new URL(document.baseURI).searchParams.getAll("c")
1 Like