File-based router for ReactPy or LilliePy framework
Project description
Lilliepy-dir-router
a router that uses folders and files to create routes for reactpy files (is used in the lilliepy framework)
Dependencies
- reactpy
- reactpy router
- pathlib
- importlib
- os
- re
Setup
if you are using the lilliepy framework, then this part should be already dont for you, if not, then read this
# your_python_file.py
from lilliepy_dir_router import FileRouter
FileRouter("/path/to/your/pages/folder", verbose=True) # verbose, if set to true, will print out the routes and status of the router
Syntax
there are 4 special types of files/directories to use in this current release
Folders
Silent Folders
silent folders are denoted as ([FOLDER_NAME]), these folders are only for grouping files together, they wont be added to said file's route
e.g :-
├── pages
│ ├── (files)
│ │ ├── file.x.py
- the url for
file.x.pywill be /file, notice how it isnt /files/file
Files
Regular Files
regular files must end in a .x.py extenstion if you want it to render, if there are functions of python files you dont want to render or want to make components that you want to import to other .x.py files and it said component shouldnt have its own route, then use the regular .py extension.
- NOTE: the router will ignore files that are not in
.x.pyextenstion
Syntax
# main.x.py
from reactpy import component, html # import component (to make function a component), and html (to make html), you can also import hooks from here if you want
@component # makes function component
def main(): # NOTE: FUNCTION'S NAME MUST BE THE SAME AS THE FILE'S NAME (THIS IS CASE SENSITIVE TOO)
return html.h1("yo") # returns <h1>yo</h1>
@component
def test(): # this wont render in this file as it's name isnt the same as the file's name, however, you can use this function in your component (the functions that has the same name as the file's name)
return html.h1("Test")
- again, note, function's name MUST be the same name as the file's name if you want it to render, you can create helper functions and/or import them into the render-ing component
+root.x.py
this special file is the root file, it will render in the root of an url, and if it kept in the first dir, then it will render in "/"
e.g :-
├── pages
│ ├── +root.x.py <-- this will render in "/"
│ ├── files
│ │ ├── +root.x.py <-- this will render in "/files", notice it renders in the root folder
│ │ ├── file.x.py
│ ├── another_example
│ │ ├── +root.x.py <-- this will render in "/another_example", notice it renders in the root folder
│ │ ├── file_two.x.py
│ │ ├── nested_folder
│ │ │ ├── +root.x.py <-- this will render in "/another_example/nested_folder"
│ │ ├── nested_folder_two
│ │ │ ├── (folder)
│ │ │ │ ├── +root.x.py <-- this will render in "/another_example/nested_folder", as the parent folder for +root.x.py is a silent folder, so it is neglected
- so in simpler terms, the
+root.x.pyfile will render in the parent folder root dir url - NOTE: it will not render in "/[PARENT_FOLDER_OF_+ROOT.X.PY_FILE]/", as this is a completely different route
- NOTE: if the parent folder is a slient folder, the +root.x.py file wont be the root file for the silent folder, it will be the root folder for the parent folder of the silent folder
+layout.x.py
this special file is the layout file, it will render (basically inject) any element into a page
Syntax :-
# +root.x.py
from reactpy import component, html
@component
def root(): # the render-ing function for a +root.x.py file MUST be called "root" (LOWER CASE, NOT UPPPER CASE)
return html.h1("home")
Slug Files
these are files which take in value(s) from the url, they are made by saying +[(FILE_NAME)].x.py
e.g :-
├── pages
│ ├── +[name].x.py
│ ├── id
│ │ ├── +[id].x.py
Syntax :-
# +[id].x.py
from reactpy import html, component
@component
# function's name must be the same as the name given inbetween the two brackets in the file's name
def id(params): # 1 parameter must be given so that this function can access the value(s) in the url, the parameter's name MUST be called params if you want it to be a slug (this is to help differeciate between slug params and other params)
return html.div([
html.h1(f"Dynamic Content for ID: {params['id']}"), #params is a dictionary, trust me, knowing that helps you alot
html.p("This page dynamically renders content based on the slug."),
])
+not_found.x.py
this page (or component) will handle all 404 error routes
Syntax :-
from reactpy import component, html
@component
def not_found(): #function's name MUST be not_found
return html.h1("Not found")
server components
these files are rendered from the server and then rendered onto the client
rules:
- it is similar to the rules of
.x.py, except that the extension name is.server.x.py - you CANNOT use hooks here, sure it will render just fine, but the hooks wont work. Basically, no interactivity
When to use .x.py and .py
you should use .x.py when:
- you want the file to render
- it dosnt contain sensitive backend code
- it dosnt have parameters other than the slug file's
paramsparameter
you should use .py when:
- you want to make a component that you want to import and not have a url of its own
- you want to do backend code
- you have more parameters than just the slug's
paramsparameter
you could think of .x.py as the client components and .py as the server components in reactjs
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file lilliepy_dir_router-5.0.1.tar.gz.
File metadata
- Download URL: lilliepy_dir_router-5.0.1.tar.gz
- Upload date:
- Size: 6.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f516f395d6433faa88cbe0b5336966f7c0d5a744f46c54e1cbf4d3812c0879fa
|
|
| MD5 |
471bd2ba8216ecf92620afa0fc9ca646
|
|
| BLAKE2b-256 |
8dedb02435dbfb5ad0ac0a0aa668e65cb4c3b1a87a9532dd55929abd12e6089c
|
File details
Details for the file lilliepy_dir_router-5.0.1-py3-none-any.whl.
File metadata
- Download URL: lilliepy_dir_router-5.0.1-py3-none-any.whl
- Upload date:
- Size: 6.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9573697cd5568f3699eef9b690c7e50837a3db45db1c05d03636bb5a4a22981a
|
|
| MD5 |
35a3609a84147e57cfbacc6c1281cef5
|
|
| BLAKE2b-256 |
21075f1e96f2af682b636d804b1736857dfa6820e1ebf115ecfe042764c1a48c
|