코드 실행 (쿼리 모드)

실행중인 스니펫

  • URI: /session/:id

  • Method: POST

Executes a snippet of user code using the specified session. Each execution request to a same session may have side-effects to subsequent executions. For instance, setting a global variable in a request and reading the variable in another request is completely legal. It is the job of the user (or the front-end) to guarantee the correct execution order of multiple interdependent requests. When the session is terminated or restarted, all such volatile states vanish.

매개변수들

매개변수

타입

설명

:id

slug

The session ID.

mode

str

상수 문자열 "query".

code

str

A string of user-written code. All non-ASCII data must be encoded in UTF-8 or any format acceptable by the session.

runId

str

특정 실행을 위한 클라이언트측 고유 식별자 문자열 입니다. 해당 실행 개념에 대한 자세한 사항은 “ref”`code-execution-model`을 참조하십시오. 문자열이 주어지지 않는다면, API 서버가 첫 번째 응답에 임의로 하나를 할당할 것이고 클라이언트는 이후 같은 실행에서 이를 사용해야만 합니다.

예시:

{
  "mode": "query",
  "code": "print('Hello, world!')",
  "runId": "5facbf2f2697c1b7"
}

응답

HTTP Status Code

설명

200 OK

The session has responded with the execution result. The response body contains a JSON object as described below.

필드

타입

result

object

Execution Result Object.

참고

사용자 코드가 예외를 제기해도, 이러한 쿼리들은 성공적인 실행으로 취급됩니다. 즉, 이 API의 실패는 유저 코드가 아닌 우리의 API 하위 시스템에 오류가 있다는 것을 의미합니다.

경고

만일 유저 코드가 시스템을 위반하려고 하거나, 충돌(e.g., “세그멘테이션 결함”)을 일으키거나, 너무 오래 실행(timeout)하면, 세션이 자동적으로 종료됩니다. 이러한 경우에, 예상보다 빠르게 "finishted" 상태로 불완전한 콘솔 로그를 얻게 됩니다. 상황에 따라 ``result.stderr``에도 구체적인 에러 정보가 포함될 수 있습니다.

우리는 다양한 Python 코드가 실행될 때 몇가지 예시 반환을 나타냅니다.

예시: 간단한 반환.

print("Hello, world!")
{
  "result": {
    "runId": "5facbf2f2697c1b7",
    "status": "finished",
    "console": [
      ["stdout", "Hello, world!\n"]
    ],
    "options": null
  }
}

예시: 런타임 에러.

a = 123
print('what happens now?')
a = a / 0
{
  "result": {
    "runId": "5facbf2f2697c1b7",
    "status": "finished",
    "console": [
      ["stdout", "what happens now?\n"],
      ["stderr", "Traceback (most recent call last):\n  File \"<input>\", line 3, in <module>\nZeroDivisionError: division by zero"],
    ],
    "options": null
  }
}

예시: 멀티미디어 결과

미디어 출력물은 실행 순서에 따라 다른 콘솔 출력물과도 혼합됩니다.

import matplotlib.pyplot as plt
a = [1,2]
b = [3,4]
print('plotting simple line graph')
plt.plot(a, b)
plt.show()
print('done')
{
  "result": {
    "runId": "5facbf2f2697c1b7",
    "status": "finished",
    "console": [
      ["stdout", "plotting simple line graph\n"],
      ["media", ["image/svg+xml", "<?xml version=\"1.0\" ..."]],
      ["stdout", "done\n"]
    ],
    "options": null
  }
}

예시: 지속적 결과

import time
for i in range(5):
    print(f"Tick {i+1}")
    time.sleep(1)
print("done")
{
  "result": {
    "runId": "5facbf2f2697c1b7",
    "status": "continued",
    "console": [
      ["stdout", "Tick 1\nTick 2\n"]
    ],
    "options": null
  }
}

당신은 빈 ``code``필드로 다른 API 쿼리를 만들어야 합니다.

{
  "result": {
    "runId": "5facbf2f2697c1b7",
    "status": "continued",
    "console": [
      ["stdout", "Tick 3\nTick 4\n"]
    ],
    "options": null
  }
}

다시.

{
  "result": {
    "runId": "5facbf2f2697c1b7",
    "status": "finished",
    "console": [
      ["stdout", "Tick 5\ndone\n"],
    ],
    "options": null
  }
}

예시: 사용자 입력

print("What is your name?")
name = input(">> ")
print(f"Hello, {name}!")
{
  "result": {
    "runId": "5facbf2f2697c1b7",
    "status": "waiting-input",
    "console": [
      ["stdout", "What is your name?\n>> "]
    ],
    "options": {
      "is_password": false
    }
  }
}

당신은 사용자 입력으로 채워진 code 필드로 다른 API 쿼리를 만들어야 합니다.

{
  "result": {
    "runId": "5facbf2f2697c1b7",
    "status": "finished",
    "console": [
      ["stdout", "Hello, Lablup!\n"]
    ],
    "options": null
  }
}

자동-완성

  • URI: /session/:id/complete

  • Method: POST

매개변수들

매개변수

타입

설명

:id

slug

The session ID.

code

str

A string containing the code until the current cursor position.

options.post

str

현재 커서 위치까지 코드가 포함된 문자열.

options.line

str

현재 라인의 내용을 포함하는 문자열

options.row

int

커서의 라인 번호(0-based)를 나타내는 정수

options.col

int

커서의 현재 라인에 있는 열 번호(0-based)를 나타내는 정수

예시:

{
  "code": "pri",
  "options": {
    "post": "\nprint(\"world\")\n",
    "line": "pri",
    "row": 0,
    "col": 3
  }
}

응답

HTTP Status Code

설명

200 OK

The session has responded with the execution result. The response body contains a JSON object as described below.

필드

타입

result

list[str]

An ordered list containing the possible auto-completion matches as strings. This may be empty if the current session does not implement auto-completion or no matches have been found.

일치를 선택하고 이를 코드 텍스트에 병합하는 것은 front-end 구현에 달려 있다.

예시:

{
  "result": [
    "print",
    "printf"
  ]
}

인터럽트

  • URI: /session/:id/interrupt

  • Method: POST

매개변수들

매개변수

타입

설명

:id

slug

The session ID.

응답

HTTP Status Code

설명

204 내용 없음

Sent the interrupt signal to the session. Note that this does not guarantee the effectiveness of the interruption.