How to Upload Data From Jupyter Notebook to a Rest-api

Using JupyterHub's REST API¶

This section will give yous data on:

  • what yous can do with the API

  • create an API token

  • add API tokens to the config files

  • make an API request programmatically using the requests library

  • learn more than about JupyterHub'south API

What yous tin practise with the API¶

Using the JupyterHub REST API, you can perform actions on the Hub, such as:

  • checking which users are agile

  • calculation or removing users

  • stopping or starting single user notebook servers

  • authenticating services

  • communicating with an individual Jupyter server's REST API

A Remainder API provides a standard style for users to become and send information to the Hub.

Create an API token¶

To send requests using JupyterHub API, you lot must pass an API token with the request.

The preferred way of generating an API token is:

This openssl control generates a potential token that can so exist added to JupyterHub using .api_tokens configuration setting in jupyterhub_config.py .

Alternatively, use the jupyterhub token command to generate a token for a specific hub user by passing the 'username':

              jupyterhub token <username>            

This command generates a random string to employ equally a token and registers it for the given user with the Hub's database.

In version 0.eight.0, a token asking page for generating an API token is available from the JupyterHub user interface:

Request API token page

API token success page

Assigning permissions to a token¶

Prior to JupyterHub two.0, there were two levels of permissions:

  1. user, and

  2. admin

where a token would always accept full permissions to practice whatever its owner could practice.

In JupyterHub 2.0, specific permissions are at present divers as 'scopes', and can be assigned both at the user/service level, and at the private token level.

This allows e.g. a user with full admin permissions to request a token with limited permissions.

Updating to admin services¶

The api_tokens configuration has been softly deprecated since the introduction of services. Nosotros have no plans to remove it, but deployments are encouraged to utilize service configuration instead.

If y'all have been using api_tokens to create an admin user and a token for that user to perform some automations, the services machinery may be a better fit. If you have the following configuration:

                                c                .                JupyterHub                .                admin_users                =                {                "service-admin"                ,}                c                .                JupyterHub                .                api_tokens                =                {                "clandestine-token"                :                "service-admin"                ,                }              

This tin can be updated to create a service, with the following configuration:

                                c                .                JupyterHub                .                services                =                [                {                # give the token a name                "name"                :                "service-admin"                ,                "api_token"                :                "hugger-mugger-token"                ,                # "admin": Truthful, # if using JupyterHub i.x                },                ]                # roles are new in JupyterHub 2.0                # prior to two.0, merely 'admin': True or Faux                # was available                c                .                JupyterHub                .                load_roles                =                [                {                "name"                :                "service-office"                ,                "scopes"                :                [                # specify the permissions the token should have                "admin:users"                ,                ],                "services"                :                [                # assign the service the above permissions                "service-admin"                ,                ],                }                ]              

The token volition have the permissions listed in the office (see [scopes][] for a list of bachelor permissions), just there volition no longer exist a user account created to business firm it. The main noticeable difference is that at that place will exist no notebook server associated with the account and the service will not show up in the various user listing pages and APIs.

Make an API asking¶

To authenticate your requests, pass the API token in the request's Authorisation header.

Utilise requests¶

Using the popular Python requests library, hither'south example code to make an API asking for the users of a JupyterHub deployment. An API Get request is fabricated, and the request sends an API token for authorization. The response contains information virtually the users:

                                import                requests                api_url                =                'http://127.0.0.i:8081/hub/api'                r                =                requests                .                get                (                api_url                +                '/users'                ,                headers                =                {                'Authorization'                :                f                'token                                {                token                }                '                ,                }                )                r                .                raise_for_status                ()                users                =                r                .                json                ()              

This instance provides a slightly more than complicated asking, nevertheless the process is very like:

                                import                requests                api_url                =                'http://127.0.0.one:8081/hub/api'                data                =                {                'name'                :                'mygroup'                ,                'users'                :                [                'user1'                ,                'user2'                ]}                r                =                requests                .                post                (                api_url                +                '/groups/formgrade-data301/users'                ,                headers                =                {                'Authorization'                :                f                'token                                {                token                }                '                ,                },                json                =                data                ,                )                r                .                raise_for_status                ()                r                .                json                ()              

The same API token can as well authorize access to the Jupyter Notebook REST API provided past notebook servers managed past JupyterHub if it has the necessary access:users:servers telescopic:

Paginating API requests¶

New in version two.0.

Pagination is bachelor through the offset and limit query parameters on list endpoints, which tin can be used to return ideally sized windows of results. Here's example lawmaking demonstrating pagination on the Get /users endpoint to fetch the first xx records.

                            import              os              import              requests              api_url              =              'http://127.0.0.one:8081/hub/api'              r              =              requests              .              go              (              api_url              +              '/users?get-go=0&limit=20'              ,              headers              =              {              "Accept"              :              "application/jupyterhub-pagination+json"              ,              "Authorization"              :              f              "token                            {              token              }              "              ,              },              )              r              .              raise_for_status              ()              r              .              json              ()            

For backward-compatibility, the default structure of listing responses is unchanged. Nevertheless, this lacks pagination data (e.grand. is in that location a next page), and so if you accept plenty users that they won't fit in the first response, it is a skilful idea to opt-in to the new paginated listing format. There is a new schema for list responses which include pagination information. Y'all can asking this past including the header:

                            Have              :              application              /              jupyterhub              -              pagination              +              json            

with your request, in which case a response will look like:

                            {              "items"              :              [              {              "proper noun"              :              "username"              ,              "kind"              :              "user"              ,              ...              },              ],              "_pagination"              :              {              "offset"              :              0              ,              "limit"              :              20              ,              "total"              :              50              ,              "next"              :              {              "offset"              :              20              ,              "limit"              :              twenty              ,              "url"              :              "http://127.0.0.1:8081/hub/api/users?limit=xx&offset=20"              }              }              }            

where the listing results (same every bit pre-2.0) volition be in items , and pagination info will be in _pagination . The side by side field will include the offset, limit, and URL for requesting the next page. adjacent will be null if at that place is no next folio.

Pagination is governed by two configuration options:

  • JupyterHub.api_page_default_limit - the page size, if limit is unspecified in the request and the new pagination API is requested (default: 50)

  • JupyterHub.api_page_max_limit - the maximum page size a request can inquire for (default: 200)

Pagination is enabled on the Go /users , Go /groups , and GET /proxy REST endpoints.

Enabling users to spawn multiple named-servers via the API¶

With JupyterHub version 0.8, support for multiple servers per user has landed. Prior to that, each user could merely launch a single default server via the API like this:

              curl -X POST -H              "Authorization: token <token>"              "http://127.0.0.1:8081/hub/api/users/<user>/server"            

With the named-server functionality, it'due south now possible to launch more than than one specifically named servers against a given user. This could be used, for instance, to launch each server based on a different prototype.

Offset you must enable named-servers by including the following setting in the jupyterhub_config.py file.

c.JupyterHub.allow_named_servers = True

If using the cipher-to-jupyterhub-k8s set-up to run JupyterHub, then instead of editing the jupyterhub_config.py file direct, you could pass the post-obit as part of the config.yaml file, every bit per the tutorial:

              hub:   extraConfig:              |              c.JupyterHub.allow_named_servers              =              True            

With that setting in place, a new named-server is activated like this:

              ringlet -Ten Mail -H              "Authorization: token <token>"              "http://127.0.0.1:8081/hub/api/users/<user>/servers/<serverA>"              gyre -X Post -H              "Authorization: token <token>"              "http://127.0.0.i:8081/hub/api/users/<user>/servers/<serverB>"            

The aforementioned servers tin can be stopped by substituting DELETE for POST above.

Some caveats for using named-servers¶

For named-servers via the API to work, the spawner used to spawn these servers will need to be able to handle the case of multiple servers per user and ensure uniqueness of names, particularly if servers are spawned via docker containers or kubernetes pods.

Learn more than about the API¶

You tin can see the full JupyterHub Residual API for details.

clintondeach1976.blogspot.com

Source: https://jupyterhub.readthedocs.io/en/stable/reference/rest.html

0 Response to "How to Upload Data From Jupyter Notebook to a Rest-api"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel