-
Notifications
You must be signed in to change notification settings - Fork 54
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
adds intro ltk lesson #368
Open
abadoni5
wants to merge
1
commit into
PyAr:main
Choose a base branch
from
abadoni5:ltk
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+290
−0
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 |
---|---|---|
|
@@ -66,3 +66,7 @@ docs/_build/ | |
target/ | ||
|
||
.vscode | ||
|
||
# Build Files | ||
.tool-versions | ||
build-info |
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
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,67 @@ | ||
=================== | ||
Introduction to LTK | ||
=================== | ||
|
||
What is LTK? | ||
------------ | ||
|
||
LTK, or Layout Toolkit, is a powerful library designed to work with PyScript. PyScript allows developers to run Python code directly in the browser, leveraging web technologies to build dynamic and responsive web applications. LTK builds on this by providing a set of tools and components that make it easy to manipulate the DOM, create layouts, and add interactive elements without needing to write JavaScript. | ||
|
||
What Can LTK Do? | ||
---------------- | ||
|
||
LTK provides a variety of components and utilities that simplify common web development tasks. Some of the key features of LTK include: | ||
|
||
- **DOM Manipulation**: Create and modify HTML elements using Python. | ||
- **Layout Components**: Use components like `Div`, `Span`, `VBox`, and `HBox` to structure your web pages. | ||
- **Event Handling**: Attach event listeners to elements and handle user interactions with Python functions. | ||
- **Styling**: Apply CSS styles to elements directly from Python code. | ||
- **Integration with Other Libraries**: Easily integrate with libraries like CodeMirror to add rich features like code editing. | ||
|
||
Creating a UI in the Browser | ||
---------------------------- | ||
|
||
Using LTK, you can create user interfaces (UI) directly in the browser with Python. This approach allows you to leverage Python's simplicity and readability to build complex web applications. Let's start with a basic "Hello, World!" example to see how LTK works. | ||
|
||
Basic "Hello, World!" Example | ||
----------------------------- | ||
|
||
Here's a brief overview of a simple "Hello, World!" example using LTK: | ||
|
||
- **Import LTK Components**: Start by importing the necessary LTK components. | ||
- **Create a Layout**: Use `VBox` to create a vertical layout. | ||
- **Add Elements**: Add a `Span` to display text and a `Button` to handle user interaction. | ||
- **Run the Code**: When the button is clicked, an alert box will display "Hello, World!". | ||
|
||
Example Code | ||
------------ | ||
|
||
.. note:: | ||
The current interpreter already handles the import statements and append for us. | ||
|
||
.. code-block:: python | ||
|
||
ltk.VBox( | ||
ltk.Span("Change me!") | ||
.css("padding", 10), | ||
|
||
ltk.Button("Hello World", lambda event: ltk.window.alert("Hello World!")) | ||
.css("margin", 10), | ||
|
||
ltk.Span("Delete me!") | ||
.css("padding", 10) | ||
) | ||
|
||
|
||
Example: Interactive Editor | ||
--------------------------- | ||
|
||
This interactive example demonstrates how to use LTK to create and manipulate HTML elements, handle user input, and dynamically update the interface. | ||
|
||
.. raw:: html | ||
|
||
<div style="display: flex; justify-content: center; align-items: center; height: 100vh;"> | ||
<iframe src="/_static/ltk_mini_editor/ltk.html" width="100%" height="600px" frameborder="0"></iframe> | ||
</div> | ||
|
||
The code behind this example uses LTK components such as `Div`, `VBox`, and `Button`, as well as integration with the CodeMirror library for the editor. By interacting with the editor, you can see how changes in the Python code immediately reflect in the web interface. |
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,67 @@ | ||
=================== | ||
Introduction to LTK | ||
=================== | ||
|
||
What is LTK? | ||
------------ | ||
|
||
LTK, or Layout Toolkit, is a powerful library designed to work with PyScript. PyScript allows developers to run Python code directly in the browser, leveraging web technologies to build dynamic and responsive web applications. LTK builds on this by providing a set of tools and components that make it easy to manipulate the DOM, create layouts, and add interactive elements without needing to write JavaScript. | ||
|
||
What Can LTK Do? | ||
---------------- | ||
|
||
LTK provides a variety of components and utilities that simplify common web development tasks. Some of the key features of LTK include: | ||
|
||
- **DOM Manipulation**: Create and modify HTML elements using Python. | ||
- **Layout Components**: Use components like `Div`, `Span`, `VBox`, and `HBox` to structure your web pages. | ||
- **Event Handling**: Attach event listeners to elements and handle user interactions with Python functions. | ||
- **Styling**: Apply CSS styles to elements directly from Python code. | ||
- **Integration with Other Libraries**: Easily integrate with libraries like CodeMirror to add rich features like code editing. | ||
|
||
Creating a UI in the Browser | ||
---------------------------- | ||
|
||
Using LTK, you can create user interfaces (UI) directly in the browser with Python. This approach allows you to leverage Python's simplicity and readability to build complex web applications. Let's start with a basic "Hello, World!" example to see how LTK works. | ||
|
||
Basic "Hello, World!" Example | ||
----------------------------- | ||
|
||
Here's a brief overview of a simple "Hello, World!" example using LTK: | ||
|
||
- **Import LTK Components**: Start by importing the necessary LTK components. | ||
- **Create a Layout**: Use `VBox` to create a vertical layout. | ||
- **Add Elements**: Add a `Span` to display text and a `Button` to handle user interaction. | ||
- **Run the Code**: When the button is clicked, an alert box will display "Hello, World!". | ||
|
||
Example Code | ||
------------ | ||
|
||
.. note:: | ||
The current interpreter already handles the import statements and append for us. | ||
|
||
.. code-block:: python | ||
|
||
ltk.VBox( | ||
ltk.Span("Change me!") | ||
.css("padding", 10), | ||
|
||
ltk.Button("Hello World", lambda event: ltk.window.alert("Hello World!")) | ||
.css("margin", 10), | ||
|
||
ltk.Span("Delete me!") | ||
.css("padding", 10) | ||
) | ||
|
||
|
||
Example: Interactive Editor | ||
--------------------------- | ||
|
||
This interactive example demonstrates how to use LTK to create and manipulate HTML elements, handle user input, and dynamically update the interface. | ||
|
||
.. raw:: html | ||
|
||
<div style="display: flex; justify-content: center; align-items: center; height: 100vh;"> | ||
<iframe src="/_static/ltk_mini_editor/ltk.html" width="100%" height="600px" frameborder="0"></iframe> | ||
</div> | ||
|
||
The code behind this example uses LTK components such as `Div`, `VBox`, and `Button`, as well as integration with the CodeMirror library for the editor. By interacting with the editor, you can see how changes in the Python code immediately reflect in the web interface. |
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,18 @@ | ||
=================== | ||
Introduction to LTK | ||
=================== | ||
|
||
|
||
.. image:: ../img/TWP10_001.jpeg | ||
:height: 14.925cm | ||
:width: 9.258cm | ||
:align: center | ||
:alt: | ||
|
||
|
||
.. toctree:: | ||
:caption: Contenido | ||
:maxdepth: 1 | ||
:numbered: | ||
|
||
TWP67_1.rst |
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,18 @@ | ||
=================== | ||
Introduction to LTK | ||
=================== | ||
|
||
|
||
.. image:: ../img/TWP10_001.jpeg | ||
:height: 14.925cm | ||
:width: 9.258cm | ||
:align: center | ||
:alt: | ||
|
||
|
||
.. toctree:: | ||
:caption: Contenido | ||
:maxdepth: 1 | ||
:numbered: | ||
|
||
TWP67_1_en.rst |
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,113 @@ | ||
<!-- working code with error handling --> | ||
|
||
<html lang="en"> | ||
<head> | ||
<title>LTK on MicroPython</title> | ||
<meta charset="UTF-8"> | ||
<meta name="viewport" content="width=device-width,initial-scale=1"> | ||
|
||
<link rel="stylesheet" href="https://pyscript.net/releases/2023.11.1/core.css"> | ||
<script type="module" src="https://pyscript.net/releases/2023.11.1/core.js"></script> | ||
|
||
<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"> | ||
|
||
<!-- Codemirror Interactive Editor --> | ||
<script src="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.35.0/codemirror.js"></script> | ||
<script src="https://cdnjs.cloudflare.com/ajax/libs/codemirror/6.65.7/mode/python/python.min.js"></script> | ||
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/codemirror/6.65.7/codemirror.min.css" > | ||
|
||
|
||
</head> | ||
<body> | ||
<script type="mpy" config='{ | ||
"files": { | ||
"https://raw.githubusercontent.com/pyscript/ltk/main/ltk/jquery.py": "ltk/jquery.py", | ||
"https://raw.githubusercontent.com/pyscript/ltk/main/ltk/widgets.py": "ltk/widgets.py", | ||
"https://raw.githubusercontent.com/pyscript/ltk/main/ltk/pubsub.py": "ltk/pubsub.py", | ||
"https://raw.githubusercontent.com/pyscript/ltk/main/ltk/__init__.py": "ltk/__init__.py", | ||
"https://raw.githubusercontent.com/pyscript/ltk/main/ltk/logger.py": "ltk/logger.py", | ||
"https://raw.githubusercontent.com/pyscript/ltk/main/ltk/ltk.js": "ltk/ltk.js", | ||
"https://raw.githubusercontent.com/pyscript/ltk/main/ltk/ltk.css": "ltk/ltk.css" | ||
} | ||
}'> | ||
import ltk | ||
|
||
class Editor(ltk.Div): | ||
classes = [ "editor" ] | ||
|
||
def __init__(self, value): | ||
ltk.Div.__init__(self) | ||
self.create(value) | ||
ltk.schedule(lambda: self.editor.refresh(value), "Refresh after load") | ||
|
||
def create(self, value): | ||
config = ltk.to_js({ | ||
"mode": { | ||
"name": "python", | ||
"version": 3, | ||
"singleLineStringErrors": False | ||
}, | ||
"lineNumbers": True, | ||
"indentUnit": 4, | ||
"matchBrackets": True, | ||
}) | ||
self.editor = ltk.window.CodeMirror(self.element[0], config) | ||
self.editor.setSize("100%", "100%") | ||
self.text(value) | ||
|
||
def text(self, text=None): | ||
return self.editor.setValue(text) if text is not None else self.editor.getValue() | ||
|
||
source = """ | ||
ltk.VBox( | ||
ltk.Span("Change me!") | ||
.css("padding", 10), | ||
|
||
ltk.Button("Hello World", lambda event: ltk.window.alert("Hello World!")) | ||
.css("margin", 10), | ||
|
||
ltk.Span("Delete me!") | ||
.css("padding", 10) | ||
|
||
) | ||
""".strip() | ||
|
||
|
||
editor = Editor(source) | ||
|
||
def show_output(): | ||
try: | ||
text = editor.text() | ||
ui = eval(text) | ||
ltk.find("#editor-output").empty().append(ui.element) | ||
except Exception as e: | ||
pass # Silently catch and ignore any errors | ||
|
||
ltk.schedule(ltk.proxy(lambda: show_output()), "show output", 1) | ||
|
||
|
||
( | ||
ltk.VBox( | ||
ltk.Div() | ||
.css("border", "1px solid gray") | ||
.css("height", 405) | ||
.attr("id", "editor-output"), | ||
ltk.Text("Edit the text below and see the changes in real-time above."), | ||
editor | ||
.on("keyup", ltk.proxy(lambda event: show_output())) | ||
.css("border", "1px solid gray") | ||
.css("height", 405) | ||
.attr("id", "editor"), | ||
) | ||
.css("width", 900) | ||
.css("font-size", 14) | ||
.attr("name", "Editor") | ||
.appendTo(ltk.window.document.body) | ||
) | ||
|
||
</script> | ||
|
||
</body> | ||
</html> |
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 @@ | ||
unknown-0-0 |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
here you should remove the / as probably it is causing that the inner page cannot be loaded in a real webservice:
Probably this works in localhost, but we need to be able to serve with a random prefix.
See this lesson for an example to use a relative path:
https://github.com/PyAr/PyZombis/blob/main/_sources/lectures/TWP05/TWP05_3.rst?plain=1#L45