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

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

Closed
12rambau opened this issue Nov 23, 2023 · 2 comments
Labels

Comments

@12rambau
Copy link
Contributor

12rambau commented Nov 23, 2023

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
Copy link
Member

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:

  • Defining a script and adding from tools import initialize_notebook; initialize_notebook() hidden to the start of each notebook.
  • Doing the same but in an .rst file and .. includeing it everywhere.
  • If either of the above is too verbose, writing an own mini-extension that adds this code in the begining of each document, although I think I'd rather tolerate the copy-paste.

@12rambau
Copy link
Contributor Author

thanks @akhmerov it makes sense!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants