Skip to content

Context API

Overview

The Context API allows the user to get information about the resources available to be requested for a session on the CANFAR Science Platform. This information can be used to configure the session to request the appropriate resources for your session.

Get context information
from canfar.context import Context

context = Context()
context.resources()
{
    "cores": {
        "default": 1,
        "defaultRequest": 1,
        "defaultLimit": 16,
        "defaultHeadless": 1,
        "options": [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16],
    },
    "memoryGB": {
        "default": 2,
        "defaultRequest": 4,
        "defaultLimit": 192,
        "defaultHeadless": 4,
        "options": [1, 2, ..., 192],
    },
    "gpus": {
        "options": [1, ..., 8],
    },
}

Bases: HTTPClient

CANFAR Context.

This class is a subclass of the HTTPClient class and inherits its attributes and methods.

Examples:

>>> from canfar.context import Context
>>> context = Context()
>>> context.resources()
Source code in canfar/context.py
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
class Context(HTTPClient):
    """CANFAR Context.

    This class is a subclass of the `HTTPClient` class and inherits its
    attributes and methods.

    Examples:
        >>> from canfar.context import Context
        >>> context = Context()
        >>> context.resources()
    """

    def resources(self) -> dict[str, Any]:
        """Get available resources from the canfar server.

        Returns:
            A dictionary of available resources.

        Examples:
            >>> from canfar.context import Context
            >>> context = Context()
            >>> context.resources()
            {'cores': {
              'default': 1,
              'defaultRequest': 1,
              'defaultLimit': 16,
              'defaultHeadless': 1,
              'options': [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]
              },
             'memoryGB': {
              'default': 2,
              'defaultRequest': 4,
              'defaultLimit': 192,
              'defaultHeadless': 4,
              'options': [1,2,4...192]
             },
            'gpus': {
             'options': [1,2, ... 28]
             }
            }
        """
        response: Response = self.client.get(url="context")
        return dict(response.json())

resources

resources()

Get available resources from the canfar server.

RETURNS DESCRIPTION
dict[str, Any]

A dictionary of available resources.

Examples:

>>> from canfar.context import Context
>>> context = Context()
>>> context.resources()
{'cores': {
  'default': 1,
  'defaultRequest': 1,
  'defaultLimit': 16,
  'defaultHeadless': 1,
  'options': [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]
  },
 'memoryGB': {
  'default': 2,
  'defaultRequest': 4,
  'defaultLimit': 192,
  'defaultHeadless': 4,
  'options': [1,2,4...192]
 },
'gpus': {
 'options': [1,2, ... 28]
 }
}
Source code in canfar/context.py
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
def resources(self) -> dict[str, Any]:
    """Get available resources from the canfar server.

    Returns:
        A dictionary of available resources.

    Examples:
        >>> from canfar.context import Context
        >>> context = Context()
        >>> context.resources()
        {'cores': {
          'default': 1,
          'defaultRequest': 1,
          'defaultLimit': 16,
          'defaultHeadless': 1,
          'options': [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]
          },
         'memoryGB': {
          'default': 2,
          'defaultRequest': 4,
          'defaultLimit': 192,
          'defaultHeadless': 4,
          'options': [1,2,4...192]
         },
        'gpus': {
         'options': [1,2, ... 28]
         }
        }
    """
    response: Response = self.client.get(url="context")
    return dict(response.json())