Skip to content

lukas-t-petan-pace-dev/Python-Link

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 

Repository files navigation

PyLink

Python no frontend usando só a tag <link>

PyLink é um framework web inovador que permite integrar arquivos Python (.py) diretamente em arquivos HTML usando a tag <link rel="pylink">, de forma simples e elegante.

Instalação

pip install pylink

Ou instale em modo de desenvolvimento:

pip install -e ".[dev]"

Uso Rápido

1. Crie um novo projeto

pylink new meu-projeto
cd meu-projeto

2. Execute o servidor de desenvolvimento

pylink dev

3. Abra no navegador

Acesse http://127.0.0.1:8000

Como Funciona

Inclua componentes Python no HTML

<!DOCTYPE html>
<html>
<head>
    <!-- Carregue o runtime -->
    <script src="/pylink-runtime.js"></script>
    
    <!-- Carregue componentes Python via <link> -->
    <link rel="pylink" href="components/counter.py" type="text/python">
</head>
<body>
    <!-- Use o componente -->
    <py-counter titulo="Meu Contador" inicial="0"></py-counter>
</body>
</html>

Crie componentes Python

# components/counter.py
from pylink import PyComponent, State, state

class Counter(PyComponent):
    count: State[int] = state(0)
    
    def __init__(self, titulo="Contador", inicial=0, **props):
        super().__init__(**props)
        self.props.titulo = titulo
        self.count.value = inicial
    
    def render(self) -> str:
        return f"""
<div class="counter">
    <h3>{self.props.titulo}</h3>
    <div class="value">{self.count.value}</div>
    <button @click="incrementar">+</button>
    <button @click="decrementar">-</button>
</div>
"""
    
    def incrementar(self):
        self.count.value += 1
    
    def decrementar(self):
        self.count.value -= 1

Comandos CLI

Comando Descrição
pylink new <nome> Cria um novo projeto
pylink dev Inicia servidor de desenvolvimento com hot reload
pylink build Gera versão estática para produção
pylink run Executa em modo produção
pylink --version Mostra a versão

Estrutura do Projeto

meu-projeto/
├── app/
│   ├── components/    # Componentes Python
│   │   └── counter.py
│   ├── services/      # Serviços
│   └── pages/         # Páginas
├── static/            # Arquivos estáticos (CSS, JS)
├── templates/         # Templates HTML
├── pylink.json        # Configuração
└── pyproject.toml

API Reference

PyComponent

Classe base para componentes.

from pylink import PyComponent

class MyComponent(PyComponent):
    def __init__(self, **props):
        super().__init__(**props)
    
    def render(self) -> str:
        return "<div>Hello World</div>"

State

Estado reativo para componentes.

from pylink import State, state

class MyComponent(PyComponent):
    # Usando decorador
    count = state(0)
    
    # Ou criando diretamente
    def __init__(self):
        self.value = State(100)

Props

Propriedades passadas via atributos HTML.

class MyComponent(PyComponent):
    def __init__(self, titulo="Default", **props):
        super().__init__(**props)
        self.props.titulo = titulo  # Acessar via self.props

Servidor FastAPI

O PyLink inclui um servidor FastAPI pronto para uso:

# app/main.py
from pylink import create_app

app = create_app(
    static_dir="static",
    templates_dir="templates",
    app_dir="app",
    debug=True
)

Execute com:

pylink run
# ou
uvicorn app.main:app --reload

Browser Support

PyLink usa Pyodide (Python em WebAssembly) e requer:

  • Chrome 80+
  • Firefox 75+
  • Safari 14.1+
  • Edge 80+

Licença

MIT License

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors