import os
from bottle import *
from random import shuffle
host = 'localhost'
host = 'localhost'
@route('/')
def main():
return template('inicio.tpl')
@post('/scramble')
def scramble():
cad = request.forms.get('cad')
sec = cad.split()
res = []
for item in sec:
k = list(item)
shuffle(k)
s = ''.join(k)
res.append(s)
return template('scrambler.tpl',{'words':res, 'lista': sec})
@post('/scramble2')
def scrambler2():
frase = request.forms.get('frase')
h = frase.split()
shuffle(h)
resp = ' '.join(h)
return template('scrambler2.tpl',{'sefra': resp, 'frase': frase})
run(host=host,port=8080,debug=True)
- <!doctype html>
- <html>
- <head>
- <title>Bottle framework</title>
- <style>
- body {background: #E0F0FF;margin: 30px 40px 0 40px}
- form {margin-left:370px;}
- h1 {background-color: silver;border: 2px solid blue; border-radius: 5px;color: navy;text-align:center;}
- h2 { margin-left: 370px; color: blue;}
- </style>
- </head>
- <body>
- <h1>Scrambler</h1><br>
- <h2>Word Scrambler</h2>
- <form name="input" action="/scramble" method="POST">
- Write word to be scrambled: <input type="text" name="cad">
- <input type="submit" value="Enviar">
- </form><br><br><br>
- <h2>Sentence Scrambler</h2>
- <form name="input" action="/scramble2" method="POST">
- Write sentence to be scrambled: <input type="text" name="frase">
- <input type="submit" value="Enviar">
- </form>
- </body>
- </html>
El formulario (plantilla) html "inicio.tpl" que recoge la entrada del usuario es el siguiente:
- <!doctype html>
- <html>
- <head>
- <title>Bottle framework</title>
- <style>
- body {background: #E0F0FF;margin: 30px 40px 0 40px}
- form {text-align:center;}
- h1 {background-color: silver;border: 2px solid blue;
border-radius: 5px;color: navy;text-align:center;}
- </style>
- </head>
- <body>
- <h1>Scramble (cruzaletras)</h1>
- <form name="input" action="/scramble" method="POST">
- Ingresa palabra a desordenar: <input type="text" name="cad">
- <input type="submit" value="Enviar">
- </form>
- </body>
- </html>
Y la parte que presenta el resultado "scrambler.tpl" es:
- <!doctype html>
- <head>
- <style>
- div {text-align: center;}
- body {background: #E0F0FF; margin: 30px 40px 0 40px}
- h1 {background-color: silver;border: 2px solid blue; border-radius: 5px;color: navy;text-align:center;}
- h2 {text-align: center;}
- span {color:blue; background-color: aquamarine;}
- </style>
- </head>
- <body>
- <h1>Scramble (cruzaletras)</h1>
- <h2>La palabra desordenada es: <span>{{palabra}}<span></h2>
- <div><a href="/"><button type="button">Regresar</button></a></div>
- </body>
El código Python toma la entrada del usuario, la convierte en una lista, la desordena y la tranforma de nuevo en texto. El resultado se envía como el parámetro "palabra".
Esta es una captura de pantalla del sitio Bottle:
Y este es el resultado final de la aplicación:
No hay comentarios.:
Publicar un comentario