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

     1  package main
     2  
     3  import (
     4  	"context"
     5  	"errors"
     6  	"flag"
     7  	"fmt"
     8  	"html"
     9  	"os"
    10  	"time"
    11  
    12  	scaffold "github.com/yunabe/lgo/jupyter/gojupyterscaffold"
    13  )
    14  
    15  var connectionFile = flag.String("connection_file", "", "")
    16  
    17  type handlers struct{}
    18  
    19  func (*handlers) HandleKernelInfo() scaffold.KernelInfo {
    20  	return scaffold.KernelInfo{
    21  		ProtocolVersion:       "5.2",
    22  		Implementation:        "GoJupyterScaffoldKernel",
    23  		ImplementationVersion: "1.2.3",
    24  		LanguageInfo: scaffold.KernelLanguageInfo{
    25  			Name: "javascript",
    26  		},
    27  		Banner: "gojupyterscaffold example",
    28  	}
    29  }
    30  
    31  func (*handlers) HandleExecuteRequest(
    32  	ctx context.Context,
    33  	r *scaffold.ExecuteRequest,
    34  	stream func(string, string),
    35  	displayData func(data *scaffold.DisplayData, update bool)) *scaffold.ExecuteResult {
    36  	var i int
    37  	tick := time.Tick(time.Second)
    38  	cancelled := false
    39  loop:
    40  	for ; i < 5; i++ {
    41  		displayData(&scaffold.DisplayData{
    42  			Data: map[string]interface{}{
    43  				"text/plain": fmt.Sprintf("Loop: i == %d", i),
    44  			},
    45  			Transient: map[string]interface{}{
    46  				"display_id": "hogehoge",
    47  			},
    48  			Metadata: map[string]interface{}{},
    49  		}, i > 0)
    50  		stream("stdout", fmt.Sprintf("hello --- %d ---", i))
    51  		stream("stderr", "world")
    52  		displayData(&scaffold.DisplayData{
    53  			Data: map[string]interface{}{
    54  				"text/html": "Hello <b>World</b>",
    55  			},
    56  			Metadata: map[string]interface{}{},
    57  		}, false)
    58  		select {
    59  		case <-tick:
    60  		case <-ctx.Done():
    61  			cancelled = true
    62  			break loop
    63  		}
    64  	}
    65  	res := &scaffold.ExecuteResult{ExecutionCount: i}
    66  	if cancelled {
    67  		res.Status = "error"
    68  		stream("stderr", "Cancel!")
    69  	} else {
    70  		res.Status = "ok"
    71  		stream("stdout", "Done!")
    72  	}
    73  	return res
    74  }
    75  
    76  func (*handlers) HandleComplete(req *scaffold.CompleteRequest) *scaffold.CompleteReply {
    77  	return &scaffold.CompleteReply{
    78  		Status:      "ok",
    79  		Matches:     []string{"abc", "xyz", "123", "こんにちは"},
    80  		CursorStart: req.CursorPos,
    81  		CursorEnd:   req.CursorPos,
    82  	}
    83  }
    84  
    85  func (*handlers) HandleInspect(req *scaffold.InspectRequest) *scaffold.InspectReply {
    86  	return &scaffold.InspectReply{
    87  		Status: "ok",
    88  		Found:  true,
    89  		Data: map[string]interface{}{
    90  			// text/plain is shown when Shift+Tab is pushed.
    91  			"text/plain": fmt.Sprintf("Code: %q, pos: %d, detail: %d", req.Code, req.CursorPos, req.DetailLevel),
    92  			// text/html is shown when an inspection window is expanded.
    93  			"text/html": fmt.Sprintf("code: <pre>%s</pre>pos: <b>%d</b><br>detail: <i>%d</i>",
    94  				html.EscapeString(req.Code), req.CursorPos, req.DetailLevel),
    95  		},
    96  	}
    97  }
    98  
    99  func (*handlers) HandleIsComplete(req *scaffold.IsCompleteRequest) *scaffold.IsCompleteReply {
   100  	return nil
   101  }
   102  
   103  func (*handlers) HandleGoFmt(req *scaffold.GoFmtRequest) (*scaffold.GoFmtReply, error) {
   104  	return nil, errors.New("not implemented")
   105  }
   106  
   107  func main() {
   108  	flag.Parse()
   109  	fmt.Printf("os.Args == %+v\n", os.Args)
   110  	fmt.Printf("connection_file == %s\n", *connectionFile)
   111  
   112  	server, err := scaffold.NewServer(context.Background(), *connectionFile, &handlers{})
   113  	if err != nil {
   114  		fmt.Fprintf(os.Stderr, "Failed to create a server: %v\n", err)
   115  		os.Exit(1)
   116  	}
   117  	server.Loop()
   118  }