Skip to content

Commit

Permalink
feat: add fmt and share handler in pluto
Browse files Browse the repository at this point in the history
Signed-off-by: peefy <[email protected]>
  • Loading branch information
Peefy committed Jul 8, 2024
1 parent 7303f31 commit 3393ed7
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 18 deletions.
60 changes: 49 additions & 11 deletions kcl-playground/app/main.py
Original file line number Diff line number Diff line change
@@ -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)
2 changes: 1 addition & 1 deletion kcl-playground/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
pluto_client
kcl_lib==0.9.0b1
kcl_lib==0.9.0
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "<iframe src=\"" + url + "\" frameborder=\"0\" style=\"width: 100%; height: 100%\"><a href=\"" + url + "\">see this code in " + window.location.host + "</a></iframe>";
Expand Down
10 changes: 5 additions & 5 deletions kcl-playground/website/static/playground/playground.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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("");
}
}
Expand Down

0 comments on commit 3393ed7

Please sign in to comment.