Mostrando las entradas con la etiqueta bottle. Mostrar todas las entradas
Mostrando las entradas con la etiqueta bottle. Mostrar todas las entradas

martes, 11 de febrero de 2014

Una sencilla aplicación con Python y Bottle

Esta aplicación usa la plataforma Bottle y código Python para desordenar una oración o las letras de una palabra:

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)

  1. <!doctype html>
  2. <html>
  3. <head>
  4.         <title>Bottle framework</title>
  5.         <style>
  6.                 body {background: #E0F0FF;margin: 30px 40px 0 40px}
  7.                 form {margin-left:370px;}
  8.                 h1 {background-color: silver;border: 2px solid blue; border-radius: 5px;color: navy;text-align:center;}
  9.                 h2 { margin-left: 370px; color: blue;}
  10.         </style>
  11. </head>
  12. <body>
  13.         <h1>Scrambler</h1><br>
  14.         <h2>Word Scrambler</h2>
  15.         <form name="input" action="/scramble" method="POST">
  16.         Write word to be scrambled: <input type="text" name="cad">
  17.         <input type="submit" value="Enviar">
  18.         </form><br><br><br>
  19.         <h2>Sentence Scrambler</h2>
  20.         <form name="input" action="/scramble2" method="POST">
  21.         Write sentence to be scrambled: <input type="text" name="frase">
  22.         <input type="submit" value="Enviar">
  23.         </form>
  24. </body>
  25. </html>


El formulario (plantilla) html "inicio.tpl" que recoge la entrada del usuario es el siguiente:

  1. <!doctype html>
  2. <html>
  3. <head>
  4.         <title>Bottle framework</title>
  5.         <style>
  6.                 body {background: #E0F0FF;margin: 30px 40px 0 40px}
  7.                 form {text-align:center;}
  8.                 h1 {background-color: silver;border: 2px solid blue;
  9.  border-radius: 5px;color: navy;text-align:center;}
  10.         </style>
  11. </head>
  12. <body>
  13.         <h1>Scramble (cruzaletras)</h1>
  14.         <form name="input" action="/scramble" method="POST">
  15.         Ingresa palabra a desordenar: <input type="text" name="cad">
  16.         <input type="submit" value="Enviar">
  17.         </form>
  18. </body>
  19. </html>



Y la parte que presenta el resultado "scrambler.tpl" es:

  1. <!doctype html>
  2. <head>
  3. <style>
  4.         div {text-align: center;}
  5.         body {background: #E0F0FF; margin: 30px 40px 0 40px}
  6.         h1 {background-color: silver;border: 2px solid blue; border-radius: 5px;color: navy;text-align:center;}
  7.         h2 {text-align: center;}
  8.         span {color:blue; background-color: aquamarine;}
  9. </style>
  10. </head>
  11. <body>
  12.         <h1>Scramble (cruzaletras)</h1>
  13.         <h2>La palabra desordenada es: <span>{{palabra}}<span></h2>
  14.         <div><a href="/"><button type="button">Regresar</button></a></div>
  15. </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: