Skip to content

Commit

Permalink
feat: enable local startup with pluto run
Browse files Browse the repository at this point in the history
  • Loading branch information
jianzs committed Jun 13, 2024
1 parent aed67a1 commit 7303f31
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 10 deletions.
5 changes: 3 additions & 2 deletions kcl-playground/app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
website = Website("./website", "kcl-playground")
router = Router("router")

website.addEnv("BACKEND_URL", router.url())

def run_code(code: str) -> kcl_api.ExecProgram_Result:
args = kcl_api.ExecProgram_Args(k_filename_list=["test.k"], k_code_list=[code])
Expand All @@ -14,7 +15,7 @@ def run_code(code: str) -> kcl_api.ExecProgram_Result:


def compile_handler(req: HttpRequest) -> HttpResponse:
code = req.body
code = req.body["body"]
result = run_code(code)
if result.err_message:
return HttpResponse(status_code=200, body=json.dumps({
Expand All @@ -29,4 +30,4 @@ def compile_handler(req: HttpRequest) -> HttpResponse:
}))


router.get("/-/play/compile", compile_handler)
router.post("/-/play/compile", compile_handler)
20 changes: 19 additions & 1 deletion kcl-playground/website/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<title>KCL Playground</title>
<link rel="stylesheet" href="/static/playground/playground-full.css">

<script src="pluto.js"></script>
<script src="/static/playground/jquery/1.8.2/jquery.min.js"></script>
<script src="/static/playground/playground-full.js"></script>

Expand Down Expand Up @@ -86,7 +87,24 @@
</div>
<div id="code-editor">
<div id="wrap">
<textarea itemprop="description" id="code" name="code" autocorrect="off" autocomplete="off" autocapitalize="off" spellcheck="false">{{printf "%s" .Snippet.Body}}</textarea>
<textarea itemprop="description" id="code" name="code" autocorrect="off" autocomplete="off" autocapitalize="off" spellcheck="false">apiVersion = "apps/v1"
kind = "Deployment"
metadata = {
name = "nginx"
labels.app = "nginx"
}
spec = {
replicas = 3
selector.matchLabels = metadata.labels
template.metadata.labels = metadata.labels
template.spec.containers = [
{
name = metadata.name
image = "${metadata.name}:1.14.2"
ports = [{ containerPort = 80 }]
}
]
}</textarea>
</div>
<div id="output"></div>
</div>
Expand Down

Large diffs are not rendered by default.

30 changes: 24 additions & 6 deletions kcl-playground/website/static/playground/playground.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,19 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

function getBackendUrl() {
var backendUrl = window.plutoEnv.BACKEND_URL;
if (/http:\/\/localhost/g.test(backendUrl)) {
var parts = backendUrl.split(":");
var port = parts[parts.length - 1];

var protocol = window.location.protocol;
var host = window.location.hostname;
backendUrl = `${protocol}//${host}:${port}`;
}
return backendUrl;
}

function HTTPTransport() {
'use strict';

Expand All @@ -17,12 +30,12 @@ function HTTPTransport() {
}
var e = events.shift();
if (e.Delay === 0) {
output({Kind: 'stdout', Body: e.Message});
output({Kind: 'stdout', Body: e.message});
next();
return;
}
timeout = setTimeout(function() {
output({Kind: 'stdout', Body: e.Message});
output({Kind: 'stdout', Body: e.message});
next();
}, e.Delay / 1000000);
}
Expand All @@ -46,7 +59,7 @@ function HTTPTransport() {
seq++;
var cur = seq;
var playing;
$.ajax(playgroundOptions.compileURL, {
$.ajax(getBackendUrl() + playgroundOptions.compileURL, {
type: 'POST',
data: {'version': 2, 'body': body},
dataType: 'json',
Expand All @@ -58,7 +71,7 @@ function HTTPTransport() {
error(output, data.Errors);
return;
}
playing = playback(output, data.Events);
playing = playback(output, data.events);
},
error: function() {
error(output, 'Error communicating with remote server.');
Expand Down Expand Up @@ -129,6 +142,11 @@ function PlaygroundOutput(el) {
var cl = 'system';
if (write.Kind == 'stdout' || write.Kind == 'stderr')
cl = write.Kind;

var m = write.Body;
if (!m) {
return;
}

if (m.indexOf('IMAGE:') === 0) {
// TODO(adg): buffer all writes before creating image
Expand Down Expand Up @@ -292,7 +310,7 @@ kclPlaygroundOptions({});
loading();
var data = {"body": body()};
data["imports"] = "true";
$.ajax(playgroundOptions.fmtURL, {
$.ajax(getBackendUrl() + playgroundOptions.fmtURL, {
data: data,
type: "POST",
dataType: "json",
Expand Down Expand Up @@ -321,7 +339,7 @@ kclPlaygroundOptions({});
if (sharing) return;
sharing = true;
var sharingData = body();
$.ajax(playgroundOptions.shareURL, {
$.ajax(getBackendUrl() + playgroundOptions.shareURL, {
processData: false,
data: sharingData,
type: "POST",
Expand Down

0 comments on commit 7303f31

Please sign in to comment.