Skip to content

Commit ba447e1

Browse files
Update counter example in README.md (#98)
* Update counter example in README.md * Mention the DomRenderer (and Pyodide) * Add helpfull links
1 parent c23e173 commit ba447e1

1 file changed

Lines changed: 58 additions & 23 deletions

File tree

README.md

Lines changed: 58 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -17,53 +17,74 @@ Write your Python interfaces in a declarative manner with plain render functions
1717
* Reactivity (made possible by leveraging [observ](https://github.com/fork-tongue/observ))
1818
* Function components
1919
* Class components with local state and life-cycle methods/hooks
20-
* Single-file components with Vue-like syntax (`.cgx` files)
20+
* Single-file components with Vue-like template syntax (`.cgx` files)
2121
* Custom renderers
2222

23-
Here is an example that shows a simple counter, made with a function component:
23+
Here is an example that shows a counter, made with a component with Vue-like syntax:
24+
25+
Contents of `counter.cgx`:
26+
```html
27+
<template>
28+
<widget>
29+
<label
30+
:text="f'Count: {count}'"
31+
/>
32+
<button
33+
text="bump"
34+
@clicked="bump"
35+
/>
36+
</widget>
37+
</template>
38+
39+
<script>
40+
import collagraph as cg
41+
42+
43+
class Counter(cg.Component):
44+
def __init__(self, *args, **kwargs):
45+
super().__init__(*args, **kwargs)
46+
self.state["count"] = 0
47+
48+
def bump(self):
49+
self.state["count"] += 1
50+
</script>
51+
```
2452

53+
Contents of `app.py`:
2554
```python
2655
from PySide6 import QtWidgets
27-
from observ import reactive
2856
import collagraph as cg
2957

30-
# Declare some reactive state
31-
state = reactive({"count": 0})
32-
33-
# Define function that adjusts the state
34-
def bump():
35-
state["count"] += 1
58+
# After importing collagraph, it's possible to import
59+
# components directly from .cgx files
60+
from counter import Counter
3661

37-
# Declare how the state should be rendered
38-
def Counter(props):
39-
return cg.h(
40-
"widget",
41-
{},
42-
cg.h("label", {"text": f"Count: {props['count']}"}),
43-
cg.h("button", {"text": "Bump", "on_clicked": bump}),
44-
)
45-
46-
# Create a Collagraph instance with a PySide renderer
62+
# Create a Collagraph instance with a PySide renderer
4763
# and register with the Qt event loop
4864
gui = cg.Collagraph(
4965
renderer=cg.PySideRenderer(),
5066
event_loop_type=cg.EventLoopType.QT,
5167
)
52-
# Render the function component into a container
68+
# Render the component into a container
5369
# (in this case the app but can be another widget)
5470
app = QtWidgets.QApplication()
55-
gui.render(cg.h(Counter, state), app)
71+
gui.render(cg.h(Counter), app)
5672
app.exec()
5773
```
5874

75+
Which looks something like this:
76+
77+
![collagraph example](https://github.com/fork-tongue/collagraph/assets/1000968/4ebae92e-d7be-48ea-b76a-c6eab8d62112)
78+
5979
For more examples, please take a look at the [examples folder](examples).
6080

61-
Currently there are two renderers:
81+
Currently there are three renderers:
6282

6383
* [PysideRenderer](collagraph/renderers/pyside_renderer.py): for rendering PySide6 applications
6484
* [PygfxRenderer](collagraph/renderers/pygfx_renderer.py): for rendering 3D graphic scenes with [Pygfx](https://github.com/pygfx/pygfx)
85+
* [DomRenderer](collagraph/renderers/dom_renderer.py): for rendering to browser DOM through [PyScript](https://pyscript.net) (or rather [Pyodide](https://pyodide.org/en/stable/))
6586

66-
It is possible to create a custom Renderer using the [Renderer](collagraph/renderers/__init__.py) interface, to render to other UI frameworks, for instance wxPython, or even the browser DOM.
87+
It is possible to create a custom Renderer using the [Renderer](collagraph/renderers/__init__.py) interface, to render to other UI frameworks, for instance wxPython.
6788

6889

6990
## Development
@@ -82,3 +103,17 @@ poetry run pytest
82103
# Install git pre-commit hooks to make sure tests/linting passes before committing
83104
poetry run pre-commit install
84105
```
106+
107+
108+
### Syntax Highlighting
109+
110+
Syntax highlighting for single-file components (`.cgx`) is supported for VSCode and Sublime Text:
111+
112+
* [CGX syntax highlight for Sublime Text](https://github.com/fork-tongue/cgx-syntax-highlight-sublime)
113+
* [CGX syntax highlight for VSCode](https://github.com/fork-tongue/cgx-syntax-highlight-vscode)
114+
115+
116+
### Formatting and linting
117+
118+
Linting cgx files is possible with a flake8 plugin: [flake8-cgx](https://github.com/fork-tongue/flake8-cgx).
119+
Formatting the contents of the script tag can be done with [black-cgx](https://github.com/fork-tongue/black-cgx).

0 commit comments

Comments
 (0)