-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1352ed1
commit d086f80
Showing
3 changed files
with
90 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
# Python Sample | ||
|
||
These samples make use of executing Python in the browser using pyioide. You can use these skills to allow you to use you python skills and make use of them in web pages and inside the extension points of the Power Platform. | ||
|
||
Lets look at some examples. | ||
|
||
## Integers | ||
|
||
{% pyodide %} | ||
1 + 1 | ||
{% endpyodide %} | ||
|
||
## String | ||
|
||
{% pyodide %} | ||
"a" + "b" | ||
{% endpyodide %} | ||
|
||
## Functions | ||
|
||
{% pyodide %} | ||
def hello(name): | ||
return "Hello " + name | ||
|
||
hello("World!") | ||
{% endpyodide %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
# _plugins/pyodide_plugin.rb | ||
module Jekyll | ||
class PyodideTag < Liquid::Block | ||
def initialize(tag_name, text, tokens) | ||
super | ||
@id = "pyodide-#{rand(1000..9999)}" | ||
end | ||
|
||
def render(context) | ||
code = super.strip | ||
<<~HTML | ||
<style> | ||
.CodeMirror { | ||
border: 1px solid #eee; | ||
height: auto; | ||
} | ||
.CodeMirror-scroll { | ||
max-height: 200px; | ||
} | ||
</style> | ||
<textarea id="#{@id}-code" rows="10" cols="50" style="width: 100%;">#{code}</textarea> | ||
<button id="#{@id}-runButton" onclick="runPython('#{@id}')" disabled>Run</button> | ||
<pre id="#{@id}-output"></pre> | ||
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/codemirror/6.65.7/codemirror.min.css"> | ||
<script src="https://cdn.jsdelivr.net/pyodide/v0.26.4/full/pyodide.js"></script> | ||
<script src="https://cdnjs.cloudflare.com/ajax/libs/codemirror/6.65.7/codemirror.min.js"></script> | ||
<script src="https://cdnjs.cloudflare.com/ajax/libs/codemirror/6.65.7/mode/python/python.min.js"></script> | ||
<script> | ||
async function loadPyodideAndPackages() { | ||
if (typeof window.pyodide === "undefined") { | ||
window.editors = []; | ||
window.pyodide = await loadPyodide({ | ||
indexURL: "https://cdn.jsdelivr.net/pyodide/v0.26.4/full/" | ||
}); | ||
} | ||
window.editors["#{@id}-code"] = CodeMirror.fromTextArea(document.getElementById('#{@id}-code'), { | ||
mode: 'python', | ||
lineNumbers: true | ||
}); | ||
document.getElementById('#{@id}-runButton').disabled = false; | ||
} | ||
loadPyodideAndPackages(); | ||
if (typeof window.runPython === "undefined") { | ||
window.runPython = async function runPython(id) { | ||
let editor = window.editors[id + '-code']; | ||
let code = editor.getValue(); | ||
let outputElement = document.getElementById(id + '-output'); | ||
try { | ||
let result = await pyodide.runPythonAsync(code); | ||
outputElement.textContent = result; | ||
} catch (err) { | ||
outputElement.textContent = err; | ||
} | ||
} | ||
} | ||
</script> | ||
HTML | ||
end | ||
end | ||
end | ||
|
||
Liquid::Template.register_tag('pyodide', Jekyll::PyodideTag) |