Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Allow text to be fetched from a url #4225

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion app.py
Original file line number Diff line number Diff line change
Expand Up @@ -567,7 +567,12 @@ def parse():
exception = ex

try:
response['Code'] = transpile_result.code
t = textwrap.dedent("""\
print(load_url('https://raw.githubusercontent.com/ebertmi/skulpt/epy_build/src/file.js'))
""")
response['Code'] = t + transpile_result.code

# response['Code'] = transpile_result.code
source_map_result = transpile_result.source_map.get_result()

for i, mapping in source_map_result.items():
Expand Down
10 changes: 10 additions & 0 deletions static/js/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@
import { incrementDebugLine, initializeDebugger, load_variables, startDebug } from './debugging';
import { localDelete, localLoad, localSave } from './local';
import { initializeLoginLinks } from './auth';

import { postJson, fetchText } from './comm';

Check failure on line 16 in static/js/app.ts

View workflow job for this annotation

GitHub Actions / build

Duplicate identifier 'postJson'.

Check failure on line 16 in static/js/app.ts

View workflow job for this annotation

GitHub Actions / build

Duplicate identifier 'postJson'.

import { postJson } from './comm';

Check failure on line 18 in static/js/app.ts

View workflow job for this annotation

GitHub Actions / build

Duplicate identifier 'postJson'.

Check failure on line 18 in static/js/app.ts

View workflow job for this annotation

GitHub Actions / build

Duplicate identifier 'postJson'.
import { LocalSaveWarning } from './local-save-warning';
import { HedyEditor, EditorType } from './editor';
import { stopDebug } from "./debugging";
Expand Down Expand Up @@ -1045,6 +1048,11 @@
return ((hasSleep) ? 20000 : 5000);
}) ()
});

(Sk as any).builtins.load_url = new Sk.builtin.func((url_text:any) => {
const ret = fetchText(url_text);
return new Sk.misceval.promiseToSuspension(ret.then(Sk.ffi.remapToPy));
});

(Sk as any).builtins.play = new Sk.builtin.func((notes:any) => {
//const now = Tone.now()
Expand Down Expand Up @@ -1083,11 +1091,13 @@
error.showWarning(ClientMessages['Transpile_warning'], ClientMessages['Empty_output']);
return;
}

if (!hasWarnings && code !== last_code) {
showSuccesMessage(); //FH nov 2023: typo in success :)
last_code = code;
}
if (cb) cb ();

}).catch(function(err) {
const errorMessage = errorMessageFromSkulptError(err) || null;
if (!errorMessage) {
Expand Down
23 changes: 22 additions & 1 deletion static/js/comm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,4 +102,25 @@ function ajaxError(err: any) {
// https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/readyState
internetError: err.readyState < 4,
});
}
}

/**
* Fetch a text file from a URL (hopefully it supports cross-origin calls)
*/
export function fetchText(url: string): Promise<string> {
// Use the fetch API, if available (TODO)
// if (window.fetch !== undefined) {
// return postJsonUsingFetch(url, data);
// }

return new Promise((ok, ko) => {
$.ajax({
type: 'GET',
url,
}).done((response: any) => {
ok(response);
}).fail((err) => {
ko(ajaxError(err));
});
});
}
Loading