1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
|
window.addEventListener('load', () => {
var input = ace.edit("inputEditor");
var output = ace.edit("outputEditor");
var configurator = ace.edit("configEditor");
[input, output, configurator].forEach((editor) => {
editor.setTheme("ace/theme/monokai");
editor.session.setMode("ace/mode/python");
editor.resize();
});
configurator.session.setMode("ace/mode/json");
function updateOutput() {
output.setValue(document.sort_code(input.getValue(), configurator.getValue()));
}
output.setReadOnly(true);
input.session.on('change', updateOutput);
configurator.session.on('change', updateOutput);
document.updateOutput = updateOutput;
});
languagePluginLoader.then(() => {
return pyodide.loadPackage(['micropip'])
}).then(() => {
pyodide.runPython(`
import micropip
from js import document
def use_isort(*args):
import isort
import json
import textwrap
print(f"Using {isort.__version__} of isort.")
def sort_code(code, configuration):
try:
configuration = json.loads(configuration or "{}")
except Exception as configuration_error:
return "\\n".join(textwrap.wrap(f"Exception thrown trying to read given configuration {configuration_error}", 40))
try:
return isort.code(code, **configuration)
except Exception as isort_error:
return "\\n".join(textwrap.wrap(f"Exception thrown trying to sort given imports {isort_error}", 40))
document.sort_code = sort_code
document.updateOutput()
micropip.install('isort').then(use_isort)`);
});
|