cherrypy textbox to variable

203 views
Skip to first unread message

Scott Samra

unread,
Jun 12, 2014, 10:19:30 AM6/12/14
to cherryp...@googlegroups.com
Hello,

I have a very simple application that runs a simple find/replace on a text document based on raw input to variables.  I would like to take this script and make it web based with text boxes instead of raw input in python.  I am extremely new to python and cherrypy...could some please point me in right direction or provide an example?

I would like a web page with text boxes --when you hit a submit button it will POST the variables to the python script.

Thank you,

Scott

Michiel Overtoom

unread,
Jun 15, 2014, 12:55:41 PM6/15/14
to cherryp...@googlegroups.com

Hi Scott,

> could some please point me in right direction or provide an example?

I'll provide you with a very basic and simple example. I hope it'll get you started!


samra.py:
-----------


import cherrypy
import os

class Samra(object):

@cherrypy.expose()
def index(self):
return """
<form method="post" action="processform">
find: <input type="text" name="find"><br>
replace: <input type="text" name="replace"><br>
<input type="submit">
"""

@cherrypy.expose()
def processform(self, find, replace):
os.system('python samrajob.py "%s" "%s"' % (find, replace))
return "done."

cherrypy.quickstart(Samra(), "/")



samrajob.py:
---------------

import sys
print "Doing the job of replacing '%s' with '%s'" % (sys.argv[1], sys.argv[2])




How to run:
-------------

1. start webapp with:

python samra.py

2. point webbrowser at http://127.0.0.1:8080/
3. fill in some values, for example, 'hello' and 'there'
4. press Submit

Look at the console. You'll see something like:

[15/Jun/2014:18:49:37] ENGINE Serving on http://127.0.0.1:8080
[15/Jun/2014:18:49:37] ENGINE Bus STARTED
127.0.0.1 - - [15/Jun/2014:18:49:47] "GET / HTTP/1.1" 200 215 "" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:29.0) Gecko/20100101 Firefox/29.0"
Doing the job of replacing 'hello' with 'there'
127.0.0.1 - - [15/Jun/2014:18:49:51] "POST /processform HTTP/1.1" 200 5 "http://127.0.0.1:8080/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:29.0) Gecko/20100101 Firefox/29.0"


Notice that 'Doing the job of replacing 'hello' with 'there'' line? That's the evidence your working script was called from the webapp.

Greetings,


Reply all
Reply to author
Forward
0 new messages