diff --git a/app.py b/app.py index 487ff7faaab..fb77a428ede 100644 --- a/app.py +++ b/app.py @@ -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(): diff --git a/static/js/app.ts b/static/js/app.ts index 0736a03ad2a..9bb4942d209 100644 --- a/static/js/app.ts +++ b/static/js/app.ts @@ -12,6 +12,9 @@ import { checkNow, onElementBecomesVisible } from './browser-helpers/on-element- import { incrementDebugLine, initializeDebugger, load_variables, startDebug } from './debugging'; import { localDelete, localLoad, localSave } from './local'; import { initializeLoginLinks } from './auth'; + +import { postJson, fetchText } from './comm'; + import { postJson } from './comm'; import { LocalSaveWarning } from './local-save-warning'; import { HedyEditor, EditorType } from './editor'; @@ -1045,6 +1048,11 @@ export function runPythonProgram(this: any, code: string, sourceMap: any, hasTur 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() @@ -1083,11 +1091,13 @@ export function runPythonProgram(this: any, code: string, sourceMap: any, hasTur 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) { diff --git a/static/js/comm.ts b/static/js/comm.ts index 5af821df9a2..9f7a3ad1895 100644 --- a/static/js/comm.ts +++ b/static/js/comm.ts @@ -102,4 +102,25 @@ function ajaxError(err: any) { // https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/readyState internetError: err.readyState < 4, }); -} \ No newline at end of file +} + +/** + * Fetch a text file from a URL (hopefully it supports cross-origin calls) + */ +export function fetchText(url: string): Promise { + // 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)); + }); + }); +}