diff --git a/kcl-playground/app/main.py b/kcl-playground/app/main.py index c188f51..dfe8614 100644 --- a/kcl-playground/app/main.py +++ b/kcl-playground/app/main.py @@ -1,33 +1,71 @@ import json -from pluto_client import Website, Router, HttpRequest, HttpResponse +import hashlib +from pluto_client import Website, KVStore, Router, HttpRequest, HttpResponse import kcl_lib.api as kcl_api +api = kcl_api.API() website = Website("./website", "kcl-playground") router = Router("router") +store = KVStore("store") 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]) - api = kcl_api.API() result = api.exec_program(args) return result +def fmt_code(code: str) -> str: + args = kcl_api.FormatCode_Args(source=code) + result = api.format_code(args) + return result.formatted + + def compile_handler(req: HttpRequest) -> HttpResponse: code = req.body["body"] result = run_code(code) if result.err_message: - return HttpResponse(status_code=200, body=json.dumps({ - "errors": result.err_message, - })) + return HttpResponse( + status_code=200, + body=json.dumps( + { + "errors": result.err_message, + } + ), + ) else: - return HttpResponse(status_code=200, body=json.dumps({ - "events": [{ - "message": result.yaml_result, - "kind": "stdout", - }], - })) + return HttpResponse( + status_code=200, + body=json.dumps( + { + "events": [ + { + "message": result.yaml_result, + "kind": "stdout", + } + ], + } + ), + ) + + +def fmt_handler(req: HttpRequest) -> HttpResponse: + code = req.body["body"] + result = fmt_code(code) + return HttpResponse(status_code=200, body=json.dumps({"body": result})) + + +def share_handler(req: HttpRequest) -> HttpResponse: + code = req.body["body"] + sha1 = hashlib.sha1() + sha1.update(code.encode("utf-8")) + id = sha1.hexdigest() + store.set(id, code) + return HttpResponse(status_code=200, body=json.dumps({"id": id})) router.post("/-/play/compile", compile_handler) +router.post("/-/play/fmt", fmt_handler) +router.post("/-/play/share", share_handler) diff --git a/kcl-playground/requirements.txt b/kcl-playground/requirements.txt index afb95a1..37cc049 100644 --- a/kcl-playground/requirements.txt +++ b/kcl-playground/requirements.txt @@ -1,2 +1,2 @@ pluto_client -kcl_lib==0.9.0b1 \ No newline at end of file +kcl_lib==0.9.0 \ No newline at end of file diff --git a/kcl-playground/website/static/playground/playground-embed.js b/kcl-playground/website/static/playground/playground-embed.js index c9a2879..ea5e1bd 100644 --- a/kcl-playground/website/static/playground/playground-embed.js +++ b/kcl-playground/website/static/playground/playground-embed.js @@ -61,7 +61,7 @@ function playgroundEmbed(opts) { return; } if (embedHTML) { - var path = "?id=" + xhr.responseText; + var path = "?id=" + xhr.id; var url = origin(window.location) + path; if (embed.prop('checked')){ url = ""; diff --git a/kcl-playground/website/static/playground/playground.js b/kcl-playground/website/static/playground/playground.js index 6117c24..8bbe4f6 100644 --- a/kcl-playground/website/static/playground/playground.js +++ b/kcl-playground/website/static/playground/playground.js @@ -67,8 +67,8 @@ function HTTPTransport() { if (seq != cur) return; if (!data) return; if (playing != null) playing.Stop(); - if (data.Errors) { - error(output, data.Errors); + if (data.errors) { + error(output, data.errors); return; } playing = playback(output, data.events); @@ -315,10 +315,10 @@ kclPlaygroundOptions({}); type: "POST", dataType: "json", success: function(data) { - if (data.Error) { - setError(data.Error); + if (data.error) { + setError(data.error); } else { - setBody(data.Body); + setBody(data.body); setError(""); } }