webknossos.client.context
¶
Authentication & Server Context¶
When interacting with a WEBKNOSSOS server, you might need to specify your user token to authenticate yourself. You can copy your token from
https://webknossos.org/auth/token
Using the same methods, you can also specify the webknossos-server if you are not using the default webknossos.org instance, as well as a timeout for network requests (default is 30 minutes).
There are the following four options to specify which server context to use:
-
Specifying the context in code using the
webknossos_context
contextmanager in awith
statement:with webknossos_context(token="my_webknossos_token"): # code that interacts with webknossos
For more information about the
with
statement and contextmanagers, please see this tutorial. -
You may specify your settings as environment variables
WK_TOKEN
andWK_URL
:3. You can also specify those environment variables in aWK_TOKEN="my_webknossos_token" python my-script.py
.env
file in your working directory. Environment variables set in the command line take precedence.# content of .env WK_TOKEN="my_webknossos_token" WK_URL="…" WK_TIMEOUT="3600" # in seconds
-
If nothing else is specified and authentication is needed, you are asked interactively for a token, which is used for subsequent interactions in the same python run as well.
Classes:
webknossos_context
¶
webknossos_context(url: Optional[str] = None, token: Optional[str] = None, timeout: Optional[int] = None)
Bases: ContextDecorator
Creates a new WEBKNOSSOS server context manager.
Can be used as a context manager with 'with' or as a decorator.
Parameters:
-
url
(Optional[str]
, default:None
) –Base URL for WEBKNOSSOS server, defaults to https://webknossos.org. Taken from previous context if not specified.
-
token
(Optional[str]
, default:None
) –Authentication token from https://webknossos.org/auth/token. Must be specified explicitly.
-
timeout
(Optional[int]
, default:None
) –Network request timeout in seconds, defaults to 1800 (30 min). Taken from previous context if not specified.
Examples:
Using as context manager:
with webknossos_context(token="my_webknossos_token"):
# code that interacts with webknossos
ds.download(...)
Using as decorator:
@webknossos_context(token="my_webknossos_token")
def my_func():
# code that interacts with webknossos
...
Note
The url and timeout parameters will use values from the previous context (e.g. environment variables) if not specified explicitly. The token parameter must always be set explicitly.
- Get Help
- Community Forums
- Email Support