Skip to main content

Nebula is a lightweight Python backend framework with middleware and routing, built on top of the werkzeug. It allows you to create web applications quickly with simple, decorator-based routing.

Project description

Nebula

PyPI Stars Contributors

Nebula is a lightweight Python backend framework with middleware and routing, built on top of the werkzeug module. It allows you to create web applications quickly with simple, decorator-based routing.

Features

  • Decorator-based Routing: Define your routes with simple and intuitive decorators.
  • Template Loading: Easily load and serve HTML templates from a designated directory.
  • Statics Loading: Easily load and serve files from designated directory.
  • Middleware Support: before_request and after_request hooks for flexible request handling.
  • Flexible Error Handling: Easily handle HTTP errors with use of error_handler(<http_code).
  • Easy to Use: Get a server up and running in just a few lines of code.

Installation

Install Nebula from PyPI:

pip install nebula-core

or clone and install from GitHub.

git clone https://github.com/VxidDev/nebula.git
cd nebula
pip install .

Usage

Here's a basic example of creating a simple web server with Nebula.

main.py:

from nebula import Nebula , Response, current_request

from nebula.utils import (
    jsonify, htmlify, load_template , render_template , render_template_string
)

app = Nebula(__file__ , "localhost", 8000, False)

app.init_all("statics")

jinja_template = """
    <h1>{{ APP.host + ":" + APP.port|string }}</h1>
"""

@app.route("/" , methods=["GET" , "POST"])
def main():
    if current_request.method == "POST":    
        data = current_request.get_json()

        return jsonify({"greet": f"Hi, {data.get('name', 'default')}!"})

    return render_template(app, "test.html")

@app.route("/greet/<name>")
def greet(name):
    return htmlify(f"<h1>Hi, {name}!</h1>")

@app.route("/fruits")
def jsonTest():
    return jsonify({
        "fruits": {
            "apples": 6,
            "pears": 10,
            "mangos": 9
        } 
    })

@app.error_handler(405)
def method_not_allowed():
    return htmlify("<h1>Cant do that :[</h1>", 405)

@app.error_handler(404)
def not_found():
    return htmlify("<h1>Cant find that :(</h1>", 404)

@app.error_handler(500)
def doesnt_work():
    return htmlify("<h1>Internal Error!</h1>", 500)

@app.route("/internal-error")
def error():
    return Response(f"Error!", 500)

@app.route("/api", methods=["POST"])
def api():
    return jsonify({"a": 1, "b": 2, "c": 3}[current_request.get_json().get("item", "a")])

@app.route("/jinja")
def jinja():
    return render_template(app, "jinja_template.html", APP=app)

@app.route("/jinja/string")
def jinja_string():
    return htmlify(render_template_string(app, jinja_template, APP=app))

wsgi = app.wsgi_app

if __name__ == "__main__":
    app.run(ssl_context=("localhost+2.pem", "localhost+2-key.pem")) # ssl is optional.

Run your app:

gunicorn -w 1 -k eventlet "main:wsgi"

Open your browser and navigate to http://localhost:8000 to see your page.

Contributions <3

amogus-gggy:

  • refactoring Nebula 0.2.0.
  • adding websockets support (2.0.0)
    Huge thank you!

License

This project is licensed under the MIT License - see the LICENSE file for details.

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

nebula_core-2.2.0.tar.gz (13.9 kB view details)

Uploaded Source

Built Distribution

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

nebula_core-2.2.0-py3-none-any.whl (13.2 kB view details)

Uploaded Python 3

File details

Details for the file nebula_core-2.2.0.tar.gz.

File metadata

  • Download URL: nebula_core-2.2.0.tar.gz
  • Upload date:
  • Size: 13.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.13

File hashes

Hashes for nebula_core-2.2.0.tar.gz
Algorithm Hash digest
SHA256 66700985ada44986fc2d73d24fbbb9cc4953f4407f1232c7573b5dbc90531c91
MD5 99aa221f7c57673f1b278d8b37be24f9
BLAKE2b-256 01d985d8f813ede0478d889a3003dad86a19cd95999168ce300e98ba7ca0d0f2

See more details on using hashes here.

File details

Details for the file nebula_core-2.2.0-py3-none-any.whl.

File metadata

  • Download URL: nebula_core-2.2.0-py3-none-any.whl
  • Upload date:
  • Size: 13.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.13

File hashes

Hashes for nebula_core-2.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 4ceebc040206b7b01d0269078e0a4573217d7d0444c531f8a788d99930c7dbcd
MD5 5fc956ba3622de6abbcb4fe5d8c9cc38
BLAKE2b-256 2bbd772c4f9dd61e05fcd9bbebfcbe7faf112a3c4dcd30c6019b8be3cbde99e7

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