Skip to main content

My first Python web framework.

Project description

PyGenUz: Python Web Framewrok build for learning purposes

purpose PyPI Version

PyGenUz is a Python web framewrok built for learning purposes.

It's a WSGI framework and can be used with any WSGI aplication server such as Gunicorn.

Installation

pip install pygenuz

How to use it

Basic usage

```
from pygenuz.app import PyGenUz

app = PyGenUz()


@app.route('/home', allowed_methods="get")
def home(request, response):
    response.text = "Hello from home page"


@app.route('/about')
def about(request, response):
    response.text = "Hello from about page"


@app.route("/hello/{name}")
def greeting(request, response, name):
    response.text = f"Hello, {name}"


@app.route('/books')
class Books:
    def get(self, request, response):
        response.text = "Books page"

    def post(self, request, response):
        response.text = "endpoint to create a book"

    def delete(self, request, response):
        response.text = "deleted"
```

How we can use Templates?

Using Templates

We have to make dir name called temps/

    @app.route("/temp")
    def template_handler(req, resp):
        resp.html = app.template(
            "home.html",
            context = {"new_title": "New title", "new_body": "New body"}
        )

How we can use json?

    @app.route("/json")
    def json_handler(req, resp):
        response_data = {"name": "some name"}
        resp.json = response_data

How we can use Static files

We have to make dir name called static/

.html

    <link rel="stylesheet" href="/static/test.css">

MODELS AND ORM

For create models you have to make only models.py and you must to write the following code.

from pygenuz.db import *

Base = declarative_base() 
Example models.py
from pygenuz.db import *

Base = declarative_base() 

class User(Base):
    __tablename__ = 'users'

    id = Column(Integer, primary_key=True)
    username = Column(String(50), nullable=False)
    email = Column(String(100), nullable=False, unique=True)
    age = Column(Integer)
    is_active = Column(Boolean, default=True)

class Book(Base):
    __tablename__ = 'books'

    id = Column(Integer, primary_key=True)
    title = Column(String(100), nullable=False)
    author = Column(String(100), nullable=False)
    published_date = Column(DateTime)
    user_id = Column(Integer, ForeignKey('users.id'))
    user = relationship('User', back_populates='books')

After writing models.py, you must to write configure.py to set the database settings

from pygenuz.configs import *
from models import Base

def configure_database():
    if Base:
        DATABASE_URL = 'sqlite:///database.db'
        engine = create_engine(DATABASE_URL)
        Session = sessionmaker(bind=engine)
        session = Session()

        Base.metadata.create_all(engine)
        return session 
    else:
        return None

if __name__ == "__main__":
    configure_database()

After writing configure.py, you need to migrate your models to the database. To do this, you need to write a command in the terminal or cmd

python configure.py 
will return you a Migrated response if successful else error

Using models.py in your views.py or main.py, you must import these codes.

from pygenuz.app import PyGenUz
from configure import *
app = PyGenUz()
session = configure_database()
List in jinja
@app.route("/books", allowed_methods=["get"])
def list_books(request, response):
    books = session.query(Book).all()
    response.html = app.template(
        "books.html",
        context={"books": books}
    )
books.html file
    <h1>Book List</h1>
    <table>
        <thead>
            <tr>
                <th>Title</th>
                <th>Author</th>
                <th>Published Date</th>
            </tr>
        </thead>
        <tbody>
            {% for book in books %}
            <tr>
                <td>{{ book.title }}</td>
                <td>{{ book.author }}</td>
                <td>{{ book.published_date }}</td>
            </tr>
            {% endfor %}
        </tbody>
    </table>
Get json data
@app.route("/api/books", allowed_methods=["get"])
def get_books(request, response):
    session = configure_database()
    books = session.query(Book).all()
    books_data = [
        {
            "id": book.id,
            "title": book.title,
            "author": book.author,
            "published_date": str(book.published_date)
        }
        for book in books
    ]
    response.json = books_data
POST json data
@app.route("/api/book", allowed_methods=["post"])
def create_book(request, response):
    session = configure_database()
    book_data = request.json
    new_book = Book(
        title=book_data.get('title'),
        author=book_data.get('author'),
        published_date=book_data.get('published_date')
    )

    session.add(new_book)
    session.commit()

    response.text = "Book created successfully"
UPDATE and DELETE json data
@app.route("/api/books/{book_id}", allowed_methods=["put", "delete"])
def book_api(request, response, book_id):
    if request.method == "PUT":
        update_book(request, response, book_id)
    elif request.method == "DELETE":
        delete_book(request, response, book_id)

def update_book(request, response, book_id):
    book_data = request.json
    book = session.query(Book).filter_by(id=book_id).first()
    if not book:
        response.text = f"Book with ID {book_id} not found"
        response.status_code = 404
        return
    book.title = book_data.get('title')
    book.author = book_data.get('author')
    book.published_date = book_data.get('published_date')
    session.commit()
    response.text = "Book updated successfully"

def delete_book(request, response, book_id):
    book = session.query(Book).filter_by(id=book_id).first()
    if not book:
        response.text = f"Book with ID {book_id} not found"
        response.status_code = 404
        return
    session.delete(book)
    session.commit()
    response.text = "Book deleted successfully"

EVERY MONDAY YOU WILL SEE A NEW BIG UPDATE UNTIL V1.0.0

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

pygenuz-0.2.5.tar.gz (6.8 kB view details)

Uploaded Source

Built Distribution

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

pygenuz-0.2.5-py2.py3-none-any.whl (6.1 kB view details)

Uploaded Python 2Python 3

File details

Details for the file pygenuz-0.2.5.tar.gz.

File metadata

  • Download URL: pygenuz-0.2.5.tar.gz
  • Upload date:
  • Size: 6.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.9.6

File hashes

Hashes for pygenuz-0.2.5.tar.gz
Algorithm Hash digest
SHA256 942017d3a92f4704fbe93181f36c5601a0f4a972f96a1418f84f448c5d99a84b
MD5 8196a7506ed424e708ba3ebb73bf8b8f
BLAKE2b-256 9111bba015b8dfd23b75b7c54b2db702b158b117a00052da127d5c0f6b7fee84

See more details on using hashes here.

File details

Details for the file pygenuz-0.2.5-py2.py3-none-any.whl.

File metadata

  • Download URL: pygenuz-0.2.5-py2.py3-none-any.whl
  • Upload date:
  • Size: 6.1 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.9.6

File hashes

Hashes for pygenuz-0.2.5-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 91fbf5a762370459f9583d7799618c72c0211c770428522d03a312dafe1faa7c
MD5 7e5942ceff3259e39344e73ac67e4aad
BLAKE2b-256 80b92682443ca0483f2a984fc8eb0d9287e2ee61010d7c2001b21c84ee2d3b96

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