Skip to content

Commit bd6703c

Browse files
committed
Add code-mirror example
1 parent 47efe9e commit bd6703c

File tree

5 files changed

+87
-5
lines changed

5 files changed

+87
-5
lines changed

examples/__init__.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,13 @@
33
from examples import app
44
from examples import canvas
55
from examples import custom
6-
from examples import documentation
6+
from examples import editor
77
from examples import dom
88
from examples import inputs
99
from examples import pitch
1010
from examples import pizza
1111
from examples import splits
1212
from examples import pubsub
13-
from examples import pydata
1413
from examples import styling
1514
from examples import svg
1615
from examples import table
@@ -26,7 +25,7 @@
2625
("examples/custom.py", custom.create()),
2726
("examples/app.py", app.create()),
2827
("examples/pubsub.py", pubsub.create()),
29-
("examples/documentation.py", documentation.create()),
28+
("examples/editor.py", editor.create()),
3029
("examples/pitch.py", pitch.create()),
3130
("examples/svg.py", svg.create()),
3231
("examples/canvas.py", canvas.create()),

examples/editor.py

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
# LTK - Copyright 2023 - All Rights Reserved - chrislaffra.com - See LICENSE
2+
3+
import ltk
4+
5+
source = """
6+
ltk.VBox(
7+
# edit this text and see what happens
8+
9+
ltk.HBox(
10+
ltk.Span("Edit the text").css("margin", 12),
11+
ltk.Span("In the editor below"),
12+
)
13+
.css("height", 40)
14+
.css("background", "lightblue")
15+
.css("border", "1px solid gray"),
16+
17+
ltk.HBox(
18+
ltk.Span("This UI will update live").css("margin", 12),
19+
ltk.Span("With every character you type!").css("margin-top", 24),
20+
)
21+
.css("height", 80)
22+
.css("background", "lightyellow")
23+
.css("border", "1px solid gray"),
24+
)
25+
""".strip()
26+
27+
def create():
28+
editor = Editor(source)
29+
30+
def show_output():
31+
print(editor.text())
32+
eval(editor.text()).appendTo(
33+
ltk.find("#editor-output")
34+
.empty()
35+
)
36+
37+
ltk.schedule(ltk.proxy(lambda: show_output()), "show output", 0.1)
38+
39+
return(
40+
ltk.VerticalSplitPane(
41+
ltk.Div()
42+
.css("border", "1px solid gray")
43+
.attr("id", "editor-output"),
44+
editor
45+
.on("keyup", ltk.proxy(lambda event: show_output()))
46+
.css("border", "1px solid gray")
47+
.attr("id", "editor"),
48+
"interactive editor"
49+
)
50+
.css("height", 805)
51+
.css("width", 400)
52+
.css("font-size", 14)
53+
.attr("name", "Editor")
54+
)
55+
56+
class Editor(ltk.Div):
57+
classes = [ "editor" ]
58+
59+
def __init__(self, value):
60+
ltk.Div.__init__(self)
61+
ltk.schedule(ltk.proxy(lambda: self.create(value)), "refresh editor", 0.1)
62+
63+
def create(self, value):
64+
config = ltk.to_js({
65+
"mode": {
66+
"name": "python",
67+
"version": 3,
68+
"singleLineStringErrors": False
69+
},
70+
"lineNumbers": True,
71+
"indentUnit": 4,
72+
"matchBrackets": True,
73+
})
74+
self.editor = ltk.window.CodeMirror(self.element[0], config)
75+
self.editor.setSize("100%", "100%")
76+
self.text(value)
77+
78+
def text(self, text=None):
79+
return self.editor.setValue(text) if text is not None else self.editor.getValue()

index.html

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@
1818
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/jquery-ui.min.js"></script>
1919
<link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.13.2/themes/base/jquery-ui.css"/>
2020

21+
<!-- Codemirror Interactive Editor -->
22+
<script src="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.35.0/codemirror.js"></script>
23+
<script src="https://cdnjs.cloudflare.com/ajax/libs/codemirror/6.65.7/mode/python/python.min.js"></script>
24+
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/codemirror/6.65.7/codemirror.min.css" >
2125

2226
<!-- Import Styles for the kitchensink demo -->
2327
<link rel="stylesheet" href="kitchensink.css">

kitchensink.toml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,9 @@ files = [
1010
"examples/__init__.py",
1111
"examples/app.py",
1212
"examples/canvas.py",
13-
"examples/documentation.py",
13+
"examples/editor.py",
1414
"examples/dom.py",
1515
"examples/inputs.py",
16-
"examples/pydata.py",
1716
"examples/tictactoe.py",
1817
"examples/tictactoe.css",
1918
"examples/pitch.py",

ltk/widgets.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -732,6 +732,7 @@ def __init__(self, first, last, key):
732732
)
733733
self.addClass(f"ltk-split-pane")
734734
self.restore()
735+
self.layout()
735736
self.on("layout", proxy(lambda *args: self.layout()))
736737
schedule(self.layout, f"layout-{self.key}")
737738
window.addEventListener("resize", proxy(lambda *args: self.layout()))

0 commit comments

Comments
 (0)