Skip to main content

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.py will 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.py extenstion
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.py file 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 params parameter

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 params parameter

you could think of .x.py as the client components and .py as the server components in reactjs

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

lilliepy_dir_router-4.0.0.tar.gz (6.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

lilliepy_dir_router-4.0.0-py3-none-any.whl (5.7 kB view details)

Uploaded Python 3

File details

Details for the file lilliepy_dir_router-4.0.0.tar.gz.

File metadata

  • Download URL: lilliepy_dir_router-4.0.0.tar.gz
  • Upload date:
  • Size: 6.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.1

File hashes

Hashes for lilliepy_dir_router-4.0.0.tar.gz
Algorithm Hash digest
SHA256 7c2ca83718a31ec68b0d61fb464fbca467dd802265e891f58d55880df8aa3a83
MD5 d6e7707b0c2c4e921007c11aa46ed620
BLAKE2b-256 7be3dfa190c42f8c858c19250a889d5a122d6041d53c8bc5babb0c7227ee9677

See more details on using hashes here.

File details

Details for the file lilliepy_dir_router-4.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for lilliepy_dir_router-4.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 eacbc3dc642355eee040823e1c180c16af58989da44f91c375bd039c11e6fc16
MD5 4088326db73ee02c0df58caed7d7d9ea
BLAKE2b-256 d7483b55011110201d33b2a6d58d2dddbf7dfe933337d1dbcd6df481307a0bbf

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page