Skip to content

Commit

Permalink
Support micropython
Browse files Browse the repository at this point in the history
  • Loading branch information
laffra committed Nov 15, 2023
1 parent de28a0b commit bbbdb90
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 66 deletions.
55 changes: 7 additions & 48 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,62 +8,21 @@
<meta name="viewport" content="width=device-width,initial-scale=1">

<!-- Import PyScript -->
<script type="module" src="https://pyscript.net/releases/2023.11.1/core.js"></script>
<link rel="stylesheet" href="https://pyscript.net/releases/2023.11.1/core.css" />
<script defer type="module" src="https://pyscript.net/releases/2023.11.1/core.js"></script>

<!-- Import jQuery -->
<script src="https://code.jquery.com/jquery-3.6.0.js"></script>
<script src="https://code.jquery.com/ui/1.13.2/jquery-ui.js"></script>
<link rel="stylesheet" href="https://code.jquery.com/ui/1.13.2/themes/base/jquery-ui.css">
<style>
#progress {
position: absolute;
overflow: hidden;
top: 0px;
z-index: 10000;
width: 100%;
height: 3px;
}

#progress img {
position: relative;
height: 100px;
width: 300%;
top: -50px;
left: -100vw;
opacity: 0.5;
}
</style>
<!-- Import Styles for the kitchen sink demo -->
<link rel="stylesheet" href="kitchen.css">
</head>
<body>
<div id="progress">
<img src="progress_line.gif">
</div>

<center>
<h1>The PyScript LTK Kitchen Sink</h1>
</center>

<!-- Include LTK itself and its examples -->
<py-config>
[[fetch]]
files = [
"ltk/__init__.py",
"ltk/jquery.py",
"ltk/widgets.py",
"ltk/ltk.js",
"ltk/ltk.css",
"examples/__init__.py",
"examples/app.py",
"examples/helloworld.py",
"examples/tictactoe.py",
"examples/tictactoe.css",
"examples/custom.py",
"examples/table.py",
]
</py-config>

<!-- Create all the examples and add them to the DOM -->
<py-script src="main.py"></py-script>
<!-- Add a progress animation. This will be removed by main.py -->
<div id="progress"><img src="progress_line.gif"></div>
<h1>The PyScript LTK Kitchen Sink</h1>
<script type="mpy" src="main.py" config="kitchen.toml"></script>
</body>
</html>
22 changes: 22 additions & 0 deletions kitchen.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#progress {
position: absolute;
overflow: hidden;
top: 0px;
z-index: 10000;
width: 100%;
height: 3px;
}

#progress img {
position: relative;
height: 100px;
width: 300%;
top: -50px;
left: -100vw;
opacity: 0.5;
}

h1 {
width: 100%;
text-align: center;
}
15 changes: 15 additions & 0 deletions kitchen.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[[fetch]]
files = [
"ltk/__init__.py",
"ltk/jquery.py",
"ltk/widgets.py",
"ltk/ltk.js",
"ltk/ltk.css",
"examples/__init__.py",
"examples/app.py",
"examples/helloworld.py",
"examples/tictactoe.py",
"examples/tictactoe.css",
"examples/custom.py",
"examples/table.py",
]
23 changes: 9 additions & 14 deletions ltk/jquery.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import json
import os
import pyodide # type: ignore
import traceback


timers = {}
Expand All @@ -31,7 +30,7 @@ def find_list(selector):
return [ elements.eq(n) for n in range(elements.length) ]

def to_js(dict):
pyodide.code.run_js("window.to_js = json => JSON.parse(json);")
js.eval("window.to_js = json => JSON.parse(json);")
return js.to_js(json.dumps(dict))

def time():
Expand Down Expand Up @@ -66,18 +65,14 @@ def post(route, data, handler):
wrapper = proxy(lambda data, status, xhr: handler(js.JSON.stringify(data)))
return jQuery.post(route, payload, wrapper, "json")

def proxy(function):
def async_proxy(function):
async def call_function(*args):
try:
result = function(*args)
try:
await result # await async callback when needed
except:
pass
except:
traceback.print_exc()
return await function(*args)
return pyodide.ffi.create_proxy(call_function)

def proxy(function):
return pyodide.ffi.create_proxy(function)

def get_url_parameter(key):
return js.URLSearchParams.new(js.document.location.search).get(key)

Expand All @@ -91,11 +86,11 @@ def push_state(url):

def inject(modulepath, *files):
types = {
".js": "<script>",
".css": "<style>",
"js": "<script>",
"css": "<style>",
}
for file in files:
_, extension = os.path.splitext(file)
extension = file.split(".")[-1]
tag = types[extension]
path = os.path.join(os.path.dirname(modulepath), file)
if not path in injected:
Expand Down
8 changes: 4 additions & 4 deletions ltk/widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def flatten(self, children):
for child in children:
if isinstance(child, Widget):
result.append(child.element)
elif inspect.isgenerator(child):
elif inspect.isgenerator(child) or type(child).__name__ == "generator":
result.extend(self.flatten(child))
elif isinstance(child, list):
result.extend(self.flatten(child))
Expand Down Expand Up @@ -80,7 +80,7 @@ class Text(Widget):

def __init__(self, html=""):
Widget.__init__(self)
self.element.html(html)
self.element.html(str(html))


class Input(Widget):
Expand Down Expand Up @@ -293,8 +293,8 @@ def show(self, element):
find("#main").css("opacity", 0.3)
(self
.appendTo(body)
.css("top", element.offset().top + 32)
.css("left", min(element.offset().left, body.width() - self.width() - 12))
.css("top", element.offset().top + 28)
.css("left", min(element.offset().left, body.width() - self.width() - 10))
.addClass("ltk-menupopup-open")
)

Expand Down

0 comments on commit bbbdb90

Please sign in to comment.