Skip to content

Basic Usage

ZABBIX edited this page Sep 16, 2023 · 24 revisions

Basic Usage πŸ’‘

Configure your Project πŸ”§

After creating your project, you'll encounter a file named "project_name.py." Inside this file, you'll find multiple configuration sections. To run the application, simply execute this file.

Base Project structure:

project_name
β”œβ”€β”€ baseapp
β”‚    β”œβ”€β”€ domain
β”‚    β”‚   └── user.py
β”‚    └── views
β”‚        β”œβ”€β”€ base.js
β”‚        └── hacker.py
β”œβ”€β”€ static
β”‚   β”œβ”€β”€ home.py
β”‚   └── base.css
└── templates
β”‚   β”œβ”€β”€ home.py
β”‚   └── hacker.py
β”œβ”€β”€ project_name.py
└── window_logo.ico

Example of setting file:

import os

from pypulse import Window, Aplication
from pypulse.Template import Template

# Defining aplication route
Aplication.Vars.APLICATION_PATH = os.path.dirname(os.path.abspath(__file__))

# Defining the templates and static files places
Template.TEMPLATE_PATH = os.path.join(
    Aplication.Vars.APLICATION_PATH, 'templates')
Template.STATIC_PATH = os.path.join(
    Aplication.Vars.APLICATION_PATH, 'static')

# Setting Aplications
# I you create a new aplication you need to add this here
Aplication.SetAplication('baseapp')
Aplication.SetAplication('user')

# BROWSER SETTINGS
APP_SETTINGS = {
    'title': 'project_name',
    'debug': False,
    'debug_file_name': 'debug.log',
    'window_size_x': 800,
    'window_size_y': 800,
    'icon_path': os.path.join(
        Aplication.Vars.APLICATION_PATH, 'window_logo.ico')
}

# Initializing Browser
browser = Window.LoadBrowser(**APP_SETTINGS)

Exploring How Applications Work in Your Project πŸ—οΈ

Applications in a PyPulse project work as modular components that help you organize and manage different parts of your web application.

  • Project Structure: When working with PyPulse applications, it's strongly advisable to embrace the Domain-Driven Design (DDD) architecture. When initiating a project, PyPulse provides a base application by default, which you have the flexibility to modify as needed. This base application primarily serves as a starting point for your "Hello World" application. The default structure it offers includes
baseapp
β”œβ”€β”€ domain
β”‚   └── user.py
└── views
    β”œβ”€β”€ home.py
    └── hacker.py
  • Registering an Application: To include an application in your project, open the project's settings file and add the following line. Make sure to place this line under the 'Setting Applications' section, typically found as # Setting Applications in the settings file.
Aplication.SetAplication('mymodule')

Exploring How Views Works πŸ”πŸ“š

In PyPulse, a view is a Python function view that takes a web request and returns a web response. Views are a fundamental part of PyPulse architecture and are responsible for handling the logic of your web application. They determine what data to display, what template to render, and how to respond to user interactions.

Here are some key points about views in PyPulse:

  1. Request Handling: Views receive HTTP requests from clients (usually web browsers) and process those requests. They contain the logic to handle various HTTP methods like GET, POST, PUT, DELETE, etc.
  2. Business Logic: Views encapsulate the business logic of your application. This can include database queries, calculations, form processing, and more.
  3. Templates: In many cases, views use templates to generate HTML responses. They pass data to the templates, which are responsible for rendering the final HTML that gets sent to the client.
  4. URL Routing: Views are connected to URLs through the URL dispatcher, that is set in the decorator.

Here's an example of a simple function-based view in PyPulse:

from pypulse.View import view
from pypulse.Template import RenderTemplate
from baseapp.domain import User

@view(name='home', path_trigger='/')
def home(request):
    user = User(type='Programmmer')

    if request.get('method') == 'POST':
        # your logic
        pass

    return RenderTemplate('home.html', {'user_type': user.type})

No matter where you place the view, as long as it's adorned with the "@view(name='name', path_trigger='path')" decorator, the view will be automatically registered within the application.

Clone this wiki locally