Skip to content

Instantly share code, notes, and snippets.

@snahor
Last active December 27, 2015 09:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save snahor/7305053 to your computer and use it in GitHub Desktop.
Save snahor/7305053 to your computer and use it in GitHub Desktop.

Uso

Crea un virtualenv:

virtualenv /tmp/qwe

Esto creara un directorio en /tmp llamado qwe

Activar el virtualenv

cd /tmp/qwe
source bin/activate
mkdir -p templates static/{js,css,img}

Dentro de qwe descargar server.py y requirements.txt y luego

pip install -r requirements.txt

Dentro de templates crear los templates, y dentro de static todo lo estatico.

Para servir el directorio (dentro de qwe):

python server.py

Esto va a servir en el puerto 8080. Para ver los templates ingresar el mismo nombre con que se crearon. Por ejemplo, si creaste foo.jade visitas http://localhost:8080/foo.jade.

Importante

Los templates usando sintaxis de jade deben tener la extension .jade y si son html .html.

PS

No crees tu virtualenv en /tmp, crealo mejor dentro de Users o Desktop donde quieras.

Los ejemplos con jade, ponlos dentro de templates.

Si por a o b no te funciona los templates que crees chequea tu identacion que sea igual en todos tus archivos. De preferencia usa 2 espacios.

!!! 5
html(lang="en")
head
title Hello from Jade
block css
body
h1.title Hello from Jade
block content
block js
bottle
pyjade
jinja2
babel
pytz
import json
from bottle import Bottle, run, static_file
from jinja2 import Environment, FileSystemLoader
from babel.dates import format_datetime, format_date
from pytz import timezone
from datetime import datetime
app = Bottle()
TZONE = timezone('America/Lima')
TEMPLATE_DIR = 'source'
STATIC_DIR = 'static'
def datetimeformat(value, format='d MMM.yyyy'):
"""Format datetime objects using LDML"""
if isinstance(value, (str, unicode)):
return value
if isinstance(value, datetime):
return format_datetime(value, format,
locale='es_PE',
tzinfo=TZONE)
else:
return format_date(value, format, locale='es_PE').title()
def to_json(value=None):
if value:
return json.dumps(value)
return ''
def url_for(*args, **kwargs):
return ''
def get_user_type():
# cambiar por cualquiera de estos:
# 'company'
# 'applicant'
# '' o None
return ''
env = Environment(
loader=FileSystemLoader(TEMPLATE_DIR),
extensions=['pyjade.ext.jinja.PyJadeExtension']
)
env.filters['datetimeformat'] = datetimeformat
env.globals['url_for'] = url_for
env.globals['get_user_type'] = get_user_type
env.filters['json'] = to_json
@app.route('/<template_name>')
def serve(template_name):
template = env.get_template(template_name)
return template.render()
@app.route('/<filepath:path>')
def serve_static(filepath):
return static_file(filepath, root=STATIC_DIR)
run(app, host='localhost', port=8080, reloader=True)
extends base.jade
block js
script(src="//netdna.bootstrapcdn.com/bootstrap/3.0.1/js/bootstrap.min.js")
block css
link(href="//netdna.bootstrapcdn.com/bootstrap/3.0.1/css/bootstrap.min.css", rel="stylesheet")
block content
table.table.table-bordered
thead
tr
th
th Nombre
th Correo
th Tipo de usuario
th Fecha de registro
th Estado
tbody
tr
td
td Foo Bar
td foo@bar.baz
td Qux
td 2013-01-01
td Dead
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment