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.
pip install pylinkOu instale em modo de desenvolvimento:
pip install -e ".[dev]"pylink new meu-projeto
cd meu-projetopylink devAcesse http://127.0.0.1:8000
<!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># 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| 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 |
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
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>"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)Propriedades passadas via atributos HTML.
class MyComponent(PyComponent):
def __init__(self, titulo="Default", **props):
super().__init__(**props)
self.props.titulo = titulo # Acessar via self.propsO 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 --reloadPyLink usa Pyodide (Python em WebAssembly) e requer:
- Chrome 80+
- Firefox 75+
- Safari 14.1+
- Edge 80+
MIT License