Repository: microsoft/torchgeo

Issue:
Add "Launch in Hub" button to tutorials
We currently have a "Open in Colab" button for our tutorials. We should also have one for the Planetary Computer. See https://planetarycomputer.microsoft.com/dataset/landsat-8-c2-l2#Example-Notebook for an example of how this works.


Discussion:
I think the URL exists to make this work, but I don't think there's a nice button to display like with "Open in Colab".
@calebrob6 can you get in touch with the Planetary Computer folks and ask if this is a feature they support for external repos? The URL seems to hardcode the compute location and I don't see a button available. The URL is also kinda long...

https://pccompute.westeurope.cloudapp.azure.com/compute/hub/login?next=%2Fcompute%2Fhub%2Fuser-redirect%2Fgit-pull%3Frepo%3Dhttps%253A%252F%252Fgithub.com%252Fmicrosoft%252FPlanetaryComputerExamples%26urlpath%3Dlab%252Fworkspaces%252Flandsat-8-c2-l2-landsat-8-c2-l2-example%252Ftree%252FPlanetaryComputerExamples%252Fdatasets%252Flandsat-8-c2-l2%252Flandsat-8-c2-l2-example.ipynb%26branch%3Dmain

I'm fine with only advertising Google Colab. Just figured Azure/Planetary Computer might like some free advertising too.
@TomAugspurger, does the Planetary Computer support a "Launch in Hub" button for external scripts? We'd like to add this button on our tutorial notebooks (e.g. https://torchgeo.readthedocs.io/en/stable/tutorials/getting_started.html).


You can use https://jupyterhub.github.io/nbgitpuller/link to generate links for any Hub that has nbgitpuller installed. So I think you want something like https://pccompute.westeurope.cloudapp.azure.com/compute/hub/user-redirect/git-pull?repo=https%3A%2F%2Fgithub.com%2Fmicrosoft%2Ftorchgeo&urlpath=lab%2Ftree%2Ftorchgeo%2Fdocs%2Ftutorials%2Fgetting_started.ipynb&branch=main.

I've noticed occasional issues with jupyterhub / the notebook server getting a bit confused, possible with running sessions. So the links from our catalog (e.g. https://planetarycomputer.microsoft.com/dataset/sentinel-2-l2a#Example-Notebook) also include a urlencoded "workspace" parameter: https://pccompute.westeurope.cloudapp.azure.com/compute/hub/user-redirect/git-pull?repo=https%3A%2F%2Fgithub.com%2Fmicrosoft%2FPlanetaryComputerExamples&urlpath=lab%2Fworkspaces%2Fsentinel-2-l2a-sentinel-2-l2a-example%2Ftree%2FPlanetaryComputerExamples%2Fdatasets%2Fsentinel-2-l2a%2Fsentinel-2-l2a-example.ipynb&branch=main

One note: gitpuller doesn't currently understand "profiles", so you'll need to tell users to select the "GPU - pytorch" profile (https://github.com/jupyterhub/nbgitpuller/issues/68)

edit: I edited the URL. The old one incorrectly had a double-`/`, which caused the clone to fail.
Thanks Tom! 

Question re the workspace parameter:

It looks like this is encoded in the "urlpath" parameter: `lab/workspaces/<some workspace name>/tree/<path/to/file.ipynb>` vs. what you get from nbgitpuller `lab/tree/<path/to/file.ipynb>`. Is this correct? If so, can `<some workspace name>` be anything, or does it have to exist on the hub server already (I see that the workspace names in the PC example URLs are specific workspace names unique to each notebook)




 


Anything unique should work.
Great, thanks again!
I think we can use https://shields.io/ to create a badge
Note that since the URL contains the "main" branch, it will suffer from the same problem as https://github.com/microsoft/torchgeo/issues/274#issuecomment-996230425. Still trying to figure this out, but no reason we can't add a badge with the same issues as Google Colab.
Got this working, will open a PR once #309 is merged.

Resolving patch:
diff --git a/docs/conf.py b/docs/conf.py
--- a/docs/conf.py
+++ b/docs/conf.py
@@ -116,18 +116,35 @@
 # TODO: width option of image directive is broken, see:
 # https://github.com/pytorch/pytorch_sphinx_theme/issues/140
 nbsphinx_prolog = """
-{% set colab = "https://colab.research.google.com" %}
+{% set host = "https://colab.research.google.com" %}
 {% set repo = "microsoft/torchgeo" %}
+{% set urlpath = "docs/" ~ env.docname ~ ".ipynb" %}
 {% if "dev" in env.config.release %}
     {% set branch = "main" %}
 {% else %}
     {% set branch = "releases/v" ~ env.config.version %}
 {% endif %}
 
-.. image:: {{ colab }}/assets/colab-badge.svg
+.. image:: {{ host }}/assets/colab-badge.svg
    :class: colabbadge
    :alt: Open in Colab
-   :target: {{ colab }}/github/{{ repo }}/blob/{{ branch }}/docs/{{ env.docname }}.ipynb
+   :target: {{ host }}/github/{{ repo }}/blob/{{ branch }}/{{ urlpath }}
+
+{% set host = "https://pccompute.westeurope.cloudapp.azure.com" %}
+{% set host = host ~ "/compute/hub/user-redirect/git-pull" %}
+{% set repo = "https%3A%2F%2Fgithub.com%2Fmicrosoft%2Ftorchgeo" %}
+{% set urlpath = "tree%2Ftorchgeo%2Fdocs%2F" %}
+{% set urlpath = urlpath ~ env.docname | replace("/", "%2F") ~ ".ipynb" %}
+{% if "dev" in env.config.release %}
+    {% set branch = "main" %}
+{% else %}
+    {% set branch = "releases%2Fv" ~ env.config.version %}
+{% endif %}
+
+.. image:: https://img.shields.io/badge/-Open%20on%20Planetary%20Computer-blue
+   :class: colabbadge
+   :alt: Open on Planetary Computer
+   :target: {{ host }}?repo={{ repo }}&urlpath={{ urlpath }}&branch={{ branch }}
 """
 
 # Disables requirejs in nbsphinx to enable compatibility with the pytorch_sphinx_theme


Test patch: