Skip to content
This repository has been archived by the owner on Aug 16, 2021. It is now read-only.

lifeinaglasshouse/Pjono

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

icon
version star contributors Fork Issues Open In Visual Studio Code

A Python Web Framework Built With Socket

High Performance • Light Weight • Not really



THE PROJECT HAS BEEN ENDED

About

Pjono is a framework for creating web server, api, and any other thing that it can do.

The reason I used Socket is that I really like Socket and it much easier to used.

Making the server receiving and sending data is easy but parsing Http Request and creating Http Response are quite a bit tricky since I don't have people that can help me. I also try to add new feature for building User Interface and I had to use Third Party Library(bs4) for parsing html code. This project made me understand how HTTP actually work. Really glad that I can finish this.

Features

  • Components

    Building User Interface with Components.py module.

    Example:

    from Pjono import HtComponents, HTML
    
    HtComponents(f"""
    <div class="container-1">
        <h1>(2+2)/5={(2+2)/5}</h1>
        <h1>2*4-(9%2)={2*4-(9%2)}</h1>
        <h1>1 is 1={1 == 1}</h1>
        <h1>"Hello" is a string={isinstance("Hello", str)}</h1>
        <h1>{", ".join(str(i) for i in range(1,11))}</h1>
    </div>
    """).render(HTML("index.html"), id="root")

    You can also append object to specific element:

    from Pjono import HtComponents, Component
    
    Parent = HtComponents("<div id='root'></div>")
    
    Parent.append(lambda c: "id" in c.attr and c.attr["id"] == "root", Component("h1","Hello, World!"))
    #<div id="root">
    #   <h1>Hello, World!</h1>
    #</div>

    Converting string or dict into Component object:

    from Pjono import Component
    
    comp = Component.StrToComponent("<h2 class='title'>Inside the hole of hell</h2>")
    # Component("h2", "Inside the hole of hell", True, **{"class":"title"})
    
    comp = Component.DictToComponent({
        "Name": "div",
        "Data": "This is div class",
        "EndTag": True,
        "Attrs":{
            "id":"Container"
        }
    })
    # Component("div", "This is div class", True, id="Container")
  • Signal and Event

    With this feature, you can execute any Python function on client side but you still need JS to make it work.

    How it Work? It simple.

    scenario

    client will send a request to the server with JS and the server will get the event name and message by headers. Server will get the function that supposed to handle the request and execute it.

    from Pjono import PjonoApp, ClientEvent, HTML
    
    # Creating PjonoApp and ClientEvent object
    app = PjonoApp("Example Server")
    client = ClientEvent(app)
    
    # Add new event
    @client.addEvent("upper")
    def upper(msg):
        return msg.upper()
    
    # Register new location
    @app.register("/")
    def index(request):
        signal, event = client.getEvent(request) # process the request
        if signal and event == "upper": # if the request is an event
            return signal # msg.upper()
        return HTML("index.html") # if not then send the index.html content

    and the JS part:

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>index</title>
        <script src="/Pjono/signal.js"></script>
    </head>
    <body>
        <div id="root">
            <input name="msg" type="text" id="inp-1"><br><br>
            <button onclick="upper()">Upper</button>
        </div>
        <script>
        const Signal = new PjSignal();
        function upper(){
            Signal.fireEvent("upper", document.getElementById("inp-1").value, function(response, status){
                var ele = document.createElement("h2");
                ele.textContent = response;
                document.getElementById("root").appendChild(ele);
            });
        }
        </script>
    </body>

Features that may be added

  • WebSocket

    want to make chat app like Discord?

  • Components.py Improvement

    More HtComponents features such as extend, insert, delete and more.

  • File upload support

    This one is kinda hard to do but I'll try to make it

Usage

for more information, read the documentation.

from Pjono import PjonoApp

app = PjonoApp("My Server")

@app.register("/")
def index(request):
    return "<h1>Hello, World</h1>"

app.launch()

License

Distributed under the MIT License. See LICENSE for more information.

Contributing

Contributions can help make this project much better. All contributions are greatly appreciated.

  1. Fork the project

  2. Create your Feature Branch

    git checkout -b feature/EpicFeature

  3. Commit your changes

    git commit -m 'cool feature that can...'

  4. Push to the Branch

    git push origin feature/EpicFeature

  5. Open a Pull Request