github.com/yunabe/lgo@v0.0.0-20190709125917-42c42d410fdf/jupyter/gojupyterscaffold/api.go (about)

     1  package gojupyterscaffold
     2  
     3  import "context"
     4  
     5  // RequestHandlers is the interface to define handlers to handle Jupyter messages.
     6  // Except for HandleGoFmt, all mesages are defined in
     7  // http://jupyter-client.readthedocs.io/en/latest/messaging.html
     8  type RequestHandlers interface {
     9  	HandleKernelInfo() KernelInfo
    10  	// HandleExecuteRequest handles execute_request.
    11  	// writeStream sends stdout/stderr texts and writeDisplayData sends display_data
    12  	// (or update_display_data if update is true) to the client.
    13  	HandleExecuteRequest(ctx context.Context,
    14  		req *ExecuteRequest,
    15  		writeStream func(name, text string),
    16  		writeDisplayData func(data *DisplayData, update bool)) *ExecuteResult
    17  	HandleComplete(req *CompleteRequest) *CompleteReply
    18  	HandleInspect(req *InspectRequest) *InspectReply
    19  	// http://jupyter-client.readthedocs.io/en/latest/messaging.html#code-completeness
    20  	HandleIsComplete(req *IsCompleteRequest) *IsCompleteReply
    21  	HandleGoFmt(req *GoFmtRequest) (*GoFmtReply, error)
    22  }
    23  
    24  // KernelInfo is a reply to kernel_info_request.
    25  type KernelInfo struct {
    26  	ProtocolVersion       string             `json:"protocol_version"`
    27  	Implementation        string             `json:"implementation"`
    28  	ImplementationVersion string             `json:"implementation_version"`
    29  	LanguageInfo          KernelLanguageInfo `json:"language_info"`
    30  	Banner                string             `json:"banner"`
    31  }
    32  
    33  // KernelLanguageInfo represents language_info in kernel_info_reply.
    34  type KernelLanguageInfo struct {
    35  	Name          string `json:"name"`
    36  	Version       string `json:"version"`
    37  	Mimetype      string `json:"mimetype"`
    38  	FileExtension string `json:"file_extension"`
    39  }
    40  
    41  // ExecuteRequest is the struct to represent execute_request.
    42  type ExecuteRequest struct {
    43  	Code         string `json:"code"`
    44  	Silent       bool   `json:"silent"`
    45  	StoreHistory bool   `json:"store_history"`
    46  	AllowStdin   bool   `json:"allow_stdin"`
    47  	StopOnError  bool   `json:"stop_on_error"`
    48  }
    49  
    50  // See http://jupyter-client.readthedocs.io/en/stable/messaging.html#request-reply
    51  type errorReply struct {
    52  	Status string `json:"status"` // status must be always "error"
    53  	Ename  string `json:"ename,omitempty"`
    54  	Evalue string `json:"evalue,omitempty"`
    55  }
    56  
    57  // InspectRequest represents inspect_request.
    58  // See http://jupyter-client.readthedocs.io/en/latest/messaging.html#introspection
    59  type InspectRequest struct {
    60  	Code      string `json:"code"`
    61  	CursorPos int    `json:"cursor_pos"`
    62  	// The level of detail desired.  In IPython, the default (0) is equivalent to typing
    63  	// 'x?' at the prompt, 1 is equivalent to 'x??'.
    64  	// The difference is up to kernels, but in IPython level 1 includes the source code
    65  	// if available.
    66  	DetailLevel int `json:"detail_level"`
    67  }
    68  
    69  // InspectReply represents inspect_reply.
    70  // See http://jupyter-client.readthedocs.io/en/latest/messaging.html#introspection
    71  type InspectReply struct {
    72  	// 'ok' if the request succeeded or 'error', with error information as in all other replies.
    73  	Status string `json:"status"`
    74  	// found should be true if an object was found, false otherwise
    75  	Found bool `json:"found"`
    76  	// data can be empty if nothing is found
    77  	Data map[string]interface{} `json:"data,omitempty"`
    78  }
    79  
    80  // CompleteRequest represents complete_request.
    81  // http://jupyter-client.readthedocs.io/en/latest/messaging.html#completion
    82  type CompleteRequest struct {
    83  	// The code context in which completion is requested
    84  	// this may be up to an entire multiline cell, such as
    85  	// 'foo = a.isal'
    86  	Code string `json:"code"`
    87  	// The cursor position within 'code' (in unicode characters) where completion is requested
    88  	CursorPos int `json:"cursor_pos"`
    89  }
    90  
    91  // CompleteReply represents complete_reply.
    92  type CompleteReply struct {
    93  	// The list of all matches to the completion request, such as
    94  	// ['a.isalnum', 'a.isalpha'] for the above example.
    95  	Matches []string `json:"matches"`
    96  
    97  	// The range of text that should be replaced by the above matches when a completion is accepted.
    98  	// typically cursor_end is the same as cursor_pos in the request.
    99  	CursorStart int `json:"cursor_start"`
   100  	CursorEnd   int `json:"cursor_end"`
   101  
   102  	// 'metadata' is omitted
   103  
   104  	// status should be 'ok' unless an exception was raised during the request,
   105  	// in which case it should be 'error', along with the usual error message content
   106  	// in other messages.
   107  	Status string `json:"status"`
   108  }
   109  
   110  // ExecuteResult represents execute_result.
   111  // See http://jupyter-client.readthedocs.io/en/latest/messaging.html#execution-results
   112  type ExecuteResult struct {
   113  	Status         string `json:"status"`
   114  	ExecutionCount int    `json:"execution_count,omitempty"`
   115  	// data and metadata are omitted because they are covered by DisplayData.
   116  }
   117  
   118  // DisplayData represents display_data defined in http://jupyter-client.readthedocs.io/en/latest/messaging.html#display-data
   119  //
   120  // Jupyter Notebook does not accept display_data with "metadata: null" (Failed validating u'type' in display_data[u'properties'][u'metadata'] in Jupyter notebook).
   121  // JupyterLab does not accept display_data if metadata is missing (Missing property 'metadata').
   122  // Thus, this package automatically sets an empty map to Metadata when it's encoded.
   123  //
   124  // c.f.
   125  // The definition of MIME-type and the right format of value:
   126  // Search for "MIME_HTML"
   127  // https://github.com/jupyter/notebook/blob/master/notebook/static/notebook/js/outputarea.js
   128  // A special handling of "application/json"
   129  // https://github.com/jupyter/jupyter_client/blob/master/jupyter_client/adapter.py
   130  type DisplayData struct {
   131  	Data      map[string]interface{} `json:"data,omitempty"`
   132  	Metadata  map[string]interface{} `json:"metadata"`
   133  	Transient map[string]interface{} `json:"transient,omitempty"`
   134  }
   135  
   136  // IsCompleteRequest represents is_complete_request.
   137  // http://jupyter-client.readthedocs.io/en/latest/messaging.html#code-completeness
   138  type IsCompleteRequest struct {
   139  	// The code entered so far as a multiline string
   140  	Code string `json:"code"`
   141  }
   142  
   143  // IsCompleteReply represents is_complete_reply.
   144  // http://jupyter-client.readthedocs.io/en/latest/messaging.html#code-completeness
   145  type IsCompleteReply struct {
   146  	// One of 'complete', 'incomplete', 'invalid', 'unknown'
   147  	Status string `json:"status"`
   148  	// If status is 'incomplete', indent should contain the characters to use
   149  	// to indent the next line. This is only a hint: frontends may ignore it
   150  	// and use their own autoindentation rules. For other statuses, this
   151  	// field does not exist.
   152  	Indent string `json:"indent"`
   153  }
   154  
   155  // GoFmtRequest is the struct to represent "go fmt" request.
   156  type GoFmtRequest struct {
   157  	Code string `json:"code"`
   158  }
   159  
   160  // GoFmtReply is the struct to represent "go fmt" reply.
   161  type GoFmtReply struct {
   162  	Status string `json:"status"`
   163  	Code   string `json:"code"`
   164  }