Perry <= A framework that let's you compose websites in Python with ease!
Perry works similar to Qt and Flutter, allowing you to create component collections. Say you want to create a div with some text and an image. To do that you'd first need to create the page:
python
from Perry import component, pageView, serve, Composite
Let's break it down:
- Component - A given element that can be added anywhere on the page
- pageView - Creates a page with a route for us, we can load styles, JS and other things into it by using the 'styles' argument
- Serve - A Flask based micro-server for Perry
- Composite - The most important part! This tells our components to build themselves recursively as well as creating the skeleton, route and debugging info.
Now let's create the page ```python Homepage = component(pageView, _Inherit = True)
Homepage <= { 'title': 'Home', # Title of the page 'path':'', # Route on the webserver, no need to include the starting / 'styles': [bootstrap], # Styles and other components, here we load bootstrap which is included in Extras 'DOM': pageView.DOM, # DOM, not yet implemented but worth using in case you want to upgrade to a newer verion of Perry later 'components': HomepageContents # A ComponentSource with our elements } ```
This page will just show up as an error as we haven't yet created our component source!
This can be done through importing ComponentSource
python
HomepageContents = ComponentSource(
DIV(
Label('Hello World!', 'h1'),
Card(
Image('Image URL'),
CardText('Sample Text which has attributes for bootstrap cards', 'p')
),
Label('Good bye!', 'h1')
)
)
You'll get something like this. The trailing comment is used for debugging
```html
Sample Text which has attributes for bootstrap cards
Running on Perry v0.9 with Debug Mode on!
```
Want to bundle together multiple elements and create a universal one? That's easy to do!
```python OurCoolNewElement = DIV( Label('Hello, I have custom stuff!', 'h1', id = 'CoolTitle'), cclass = 'NewElement' )
ourCustomStyle = style() ourCustomStyle <= { 'author':'HUSKI3', 'source':'Local-made ;)' 'ctype':'css', 'css' : ''' .NewElement { color: white; background: black; } '''}
HomepageContents = ComponentSource( DIV( Label('Hello World!', 'h1'), OurCoolNewElement , # <--- here Label('Good bye!', 'h1') ) ) ```
You'll need to load the style when defining the homepage contents!
python
Homepage <= {
...,
'styles': [some, styles, ourCustomStyle],
...
}
At the moment JS doesn't have direct support through built in components, but you can use JQueryEngine
and JQueryEngineStrapper
from Extras.
```python
js = JQueryEngine(pageView, cid = 'coolscript')
js <= ( open('PerryApp/coolscript.js','r').read() )
HomepageContents.add( js ) ```
In Perry you always serve pages as a Composite collection, this way they are built and then loaded on Flask on the specified routes.
```python
serve <= Composite(Homepage, About, OtherPage, debug = True) ```
```python from fastapi import FastAPI, Response
app = FastAPI() pages = Composite(Homepage, About, OtherPage, debug = True)
print(pages)
print(pages.get(''))
@app.get("/") def read_root(): return Response(content=pages.get('').run(), media_type="text/html") ```
Hey, I love the concept and implementation of this package, but I'm not fond of Flask. Would it be possible to remove flask as a dependency and instead abstract out the routing system so it can be plugged into any framework? Specifically I'd love to use this with FastAPI.
Back-end Dev. Forma is the best. Check out @RocKing1001 for his cool projects!
GitHub Repository