jupyter / jupyter-sphinx

Sphinx extension for rendering of Jupyter interactive widgets.
https://jupyter-sphinx.readthedocs.io/
BSD 3-Clause "New" or "Revised" License
186 stars 65 forks source link

Is it possible to use the same variable in all jupyter-execute directive of my documentation ? #235

Closed 12rambau closed 8 months ago

12rambau commented 9 months ago

I would like to use the jupyter-execute directive to display maps using Google earth engine based images. My main issue is that authenticating to GEE is a complex function that I need to set at the start of every single directive:

    # only do the initialization if the credential are missing
    if not ee.data._credentials:

        # if the credentials token is asved in the environment use it
        if "EARTHENGINE_TOKEN" in os.environ:

            # write the token to the appropriate folder
            ee_token = os.environ["EARTHENGINE_TOKEN"]
            credential_folder_path = Path.home() / ".config" / "earthengine"
            credential_folder_path.mkdir(parents=True, exist_ok=True)
            credential_file_path = credential_folder_path / "ee-private-key.json"
            credential_file_path.write_text(ee_token)

            # connect to the service account
            credentials = ee.ServiceAccountCredentials("", str(credential_file_path))
            ee.Initialize(credentials=credentials, http_transport=httplib2.Http())

        else:
            # with suppress(Exception):
            AuthEE().authenticate("earth-engine-ldc-rs-test")

Is there a way to make it once and for all (e.g. in index.rst) and make sure I use the same kernel everywhere ?

A minimal example would be to share a variable between 2 executions:


.. jupyter-execute:: 

    a = 5
    a

.. jupyter-execute:: 

    a += 5
    a

Thanks for your help

akhmerov commented 9 months ago

I believe that using a standard logic for evaluation of code is a useful design constraint for us. So far jupyter-sphinx follows the concept of "like a notebook" sequential execution, which makes it easy to understand. In your minimal example, I believe, the state will be shared if the two directives happen within the same document (or more specifically within the same jupyter-kernel directive in one document.

My own solution for initialization code that goes into all documents would be some flavor of the following:

12rambau commented 8 months ago

thanks @akhmerov it makes sense!