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

Add anchors to filepath? #850

Closed
kteich-oreilly opened this issue May 21, 2019 · 6 comments
Closed

Add anchors to filepath? #850

kteich-oreilly opened this issue May 21, 2019 · 6 comments

Comments

@kteich-oreilly
Copy link

Is it possible to, when using filepath= to open a specific file, specify a section/anchor to jump to? For example, if I have this in my file myfile.ipynb:

<a id='section2'></a>

I'd love to be able to use filepath=myfile.ipynb#section2 to open that file to that anchor.

It looks like something currently sees # in filepath and redirects me to a prompt to enter a token or password. I've tried various things with urlpath as well.

Thanks!

@betatim
Copy link
Member

betatim commented May 22, 2019

Hi.

My guess is that you'd have to escape/encode the # so that it becomes part of the value passed to filepath instead of being interpreted by your browser as part of the launch URL itself. Have you tried that?

@kteich-oreilly
Copy link
Author

Him Tim, thanks for the reply.

The actual URL generated by the BinderHub portal form looks like this:
http://...?filepath=myfile.ipynb%23section2
So the # is being escaped. But following it leads to the prompt to enter a token or password.

If I use another arbitrary character like pipe, the URL looks like:
http://...?filepath=myfile.ipynb%7Csection2
and it leads to a 404 page, but the server is started. If I then manually change the resulting URL from:
http://.../tree/myfile.ipynb%7Csection2
to
http://.../tree/myfile.ipynb#section2
it loads the right file and jumps to the right section.

So it seems like the escaped # is doing something specific to lead to the prompt for a token. I'm not sure exactly what that page is doing, we'll dig into that. But it seems like this is almost working, so I'm wondering if I'm doing something wrong. Or, if it's something that could be working with a minor change, we'll look into that.

Thanks!

@bitnik
Copy link
Collaborator

bitnik commented May 28, 2019

BinderHub uses a token to access to the notebook server for each launch (for each temporary user). Just before redirecting to user server, it logs "server running at .../tree?token=xyz". But in your case server url becomes "/tree/myfile.ipynb#section2?token=xyz" and because token parameter is behing #, it is ignored and you are redirected to login page. So I think it is now not possible to have a file path with anchor.

To have it working the server url must be generated as "/tree/myfile.ipynb?token=xyz#section2" and I think this can be achived by updating here:

BinderImage.prototype.launch = function(url, token, path, pathType) {
// redirect a user to a running server with a token
if (path) {
// strip trailing /
url = url.replace(/\/$/, "");
// trim leading '/'
path = path.replace(/(^\/)/g, "");
if (pathType === "file") {
// trim trailing / on file paths
path = path.replace(/(\/$)/g, "");
// /tree is safe because it allows redirect to files
// need more logic here if we support things other than notebooks
url = url + "/tree/" + encodeURI(path);
} else {
// pathType === 'url'
url = url + "/" + path;
}
}
var sep = url.indexOf("?") == -1 ? "?" : "&";
url = url + sep + $.param({ token: token });
window.location.href = url;
};

@kteich-oreilly
Copy link
Author

Thanks Kenan, that's super helpful.

@fabiencelier
Copy link

I had the same issue with a urlpath redirect containing a # on mybinder
I try to redirect directly to a running app that uses react hash router

@manics
Copy link
Member

manics commented Sep 20, 2021

Hi 👋 I think this is something that has to be handled by the application (JupyterLab or notebook), not BinderHub. If you have more questions please use the Jupyter community forum https://discourse.jupyter.org/.

@manics manics closed this as completed Sep 20, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants