https://github.com/butvinm/markpy
Embedding tree markup syntax (XML) into Python.
https://github.com/butvinm/markpy
Last synced: 2 months ago
JSON representation
Embedding tree markup syntax (XML) into Python.
- Host: GitHub
- URL: https://github.com/butvinm/markpy
- Owner: butvinm
- License: mit
- Created: 2023-12-09T19:08:52.000Z (over 1 year ago)
- Default Branch: master
- Last Pushed: 2023-12-11T07:14:28.000Z (over 1 year ago)
- Last Synced: 2023-12-12T07:49:38.076Z (over 1 year ago)
- Language: Python
- Homepage:
- Size: 6.84 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-python-html - butvinm/markpy
- awesome-python-html - butvinm/markpy
README
# MarkPy
Embedding tree markup syntax (similar to HTML, XML) into Python.
## Example
Define your custom component class, e.g., a simple HTML `div`, by inheriting from `Component`:
```python
class Div(Component):
def __init__(self, **kwargs):
self.__dict__.update(kwargs)
```Now you can create an elements tree with a quite expressive syntax:
```python
tree = Div(class_='d1')[
Div[Div['Hello']],
Div[Div['dear']],
Div[Div['World!']],
Div['this', 'is', 'the', 'real', 'magic!']
]
```...and even print it as XML:
```python
print(tree)
``````xml
Hello
dear
World!
this
is
the
real
magic!
```## Use Cases and Motivation
The inspiration for creating this library comes from component-based libraries such as `Kyvi`, `FastUI`, `PyWebIO`, `PyWebView`, `Flexx`, etc.
Here's an example of how `MarkPy` can make `FastUI` components less redundant:
Current component declaration:
```python
c.Div(
components=[
c.Heading(text='Link List', level=2),
c.Markdown(
text=(
'This is a simple unstyled list of links, '
'LinkList is also used in `Navbar` and `Pagination`.'
)
),
c.LinkList(
links=[
c.Link(
components=[c.Text(text='Internal Link - go to the home page')],
on_click=GoToEvent(url='/'),
),
c.Link(
components=[c.Text(text='Pydantic (External link)')],
on_click=GoToEvent(url='https://pydantic.dev'),
),
],
),
],
class_name='border-top mt-3 pt-1',
)
```...and within `MarkPy`:
```python
c.Div(class_name='border-top mt-3 pt-1')[
c.Heading(level=2)['Link List'],
c.Markdown[
'This is a simple unstyled list of links, '
'LinkList is also used in `Navbar` and `Pagination`.'
],
c.LinkList[
c.Link(on_click=GoToEvent(url='/'))[
c.Text['Internal Link - go to the home page']
],
c.Link(on_click=GoToEvent(url='https://pydantic.dev'))[
c.Text['Pydantic (External link)']
],
],
]
```Is it more readable and has fewer indentations and wrappings, isn't it?