python-link
v1.90.0
Published
Python no frontend usando só a tag <link>
Maintainers
Readme
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 pylinkOu instale em modo de desenvolvimento:
pip install -e ".[dev]"Uso Rápido
1. Crie um novo projeto
pylink new meu-projeto
cd meu-projeto2. Execute o servidor de desenvolvimento
pylink dev3. 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 -= 1Comandos 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.tomlAPI 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.propsServidor 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 --reloadBrowser Support
PyLink usa Pyodide (Python em WebAssembly) e requer:
- Chrome 80+
- Firefox 75+
- Safari 14.1+
- Edge 80+
Licença
MIT License
