[Python-au] Re: python-au Digest, Vol 5, Issue 9
Duane Hennessy
duaneh at connexus.net.au
Sat Nov 29 13:51:15 CET 2003
James,
I developed a python program to run through a web browser (actually
IE 5.5) so what I did was create a Python COM which provided functions
to the web page using VBScript to access the COM. Basically I had a
python program set up in the background which ran on its own fine but
created a Python COM interface to receive data via VBScript in the page
and send back results as HTML code or delimited strings which I then
broke down using Javascript (you could use VBScript but I prefer
Javascript for web pages). Some quick example code would be like the
following.
######## web page code starts here ########
<script language = 'VBScript'>
'Create the object for the page to use
Dim Warehouse
Set Warehouse = CreateObject("Warehouse.Application") ' Python COM
set up
Sub ShowItems()
'Show chosen items. I just wrote in trousers but you would
capture this via a web page control etc.
window.innerHTML = Warehouse.show_items("trousers")
End Sub
Sub SaveItems(Item, SizeOne, SizeTwo)
'Save an item and it's sizes etc. Example of sending data to a
COM to use.
Warehouse.save_sizes(Item,SizeOne,SizeTwo)
End Sub
</script>
######## web page code ends here ########
######## python COM code starts here ########
class Inventory:
'Class registered as a COM for translating requests and returns to a
web page'
_public_methods_ = ['show_items','save_sizes'] # list of methods
you want to make available to programs using the COM.
_reg_progid_ = 'Warehouse.Application' # the COM id which we
load in the web page
_reg_clsid_ = '{1562C973-CBEB-469D-9A96-54C6CF44490B}' # use
pythoncom.CreateGuid() to get the class id.
def show_items(self,w_item):
# note if you pass data via a web page it will be in Unicode
format and causes Python to cause errors so I convert the passed arguments
# before doing anything further with them. If you are not
passing arguments though you don't need this obviously.
w_item = str(w_item);
# set up a variable to return the HTML code to return to the web
page
html_return = '<table id = \'return_items\'>'
# use a method to capture whatever data you want to return and
write it into a web code format eg, table.
# code to get items here... eg maybe you are dragging the item
from a database etc.
for item_name in item_list:
html_return += '<tr>
html_return += '<td>%s</td>' % item_name
html_return += '</tr>'
# return the result to the web page
html_return += '</table>'
return html_return
def save_sizes(item,size_one,size_two):
# note if you pass data via a web page it will be in Unicode
format and causes Python to cause errors so I convert the passed arguments
# before doing anything further with them.
item = str(item)
size_one = int(size_one)
size_two = int(size_two)
# code to save the item sizes here, could be a database or
anything, you get it though.
if __name__ == '__main__':
print 'Registering COM server'
import win32com.server.register
win32com.server.register.UseCommandLine(Inventory) # note the
command line is the name of the class
######## python COM code ends here ########
Basically that is it. Things to note are, the web page will pass any
arguments you pass to the Python COM in unicode format and I recommend
that you convert them as a first step in the method should you have any
errors during the Python code. Can't remember the error I received and I
didn't receive it for all methods in which I passed strings, integers to
a Python method via the COM only a few. Check it out anyway.
Basically any changes you make to the rest of the programs modules
are automatically reflected via the COM, only changes to the COM
interface need to be re-registered if they include new methods you want
made available to the Web page, other changes are reflected
automatically, like changes in algorithm etc.
Hope this helps, let me know if you have any problems implementing this
into the web page and I'll see what I can do. It wasn't plain sailing
when I first attempted this but works like a dream now.
Duane Hennessy
Tropical Queensland, Australia
>Message: 2
>Date: Fri, 21 Nov 2003 15:37:58 +1100
>From: "Jimmy" <jbou5922 at bigpond.net.au>
>Subject: [Python-au] Request for advice
>To: <python-au at python.net>
>Message-ID: <017001c3afe9$3d4579f0$0100000a at JIMSMACHINE>
>Content-Type: text/plain; charset="iso-8859-1"
>
>Hi,
>
>I'm currently developing a prototype in Python, that has the potential to
>make the transition to being part of a long term commercial development. The
>prototype basically performs all of the business logic thats required, but
>simply takes interactively typed or text file based input and simply prints
>the required data. What's required is simply to plug the input and output
>information into some sort of form, preferably browser based. Can anyone
>point me to any documentation that outlines the techniques for connecting
>python classes and routines to web-based forms. This software will initially
>run standalone, so embedding this code in a Zope style framework is probably
>going to be an overkill. This is a pretty time critical development, so I'd
>prefer an approach that's as simple and easy as possible.
>
>Any advice on this, or knowledge of suitable products that make this process
>easy would be greatly appreciated. I'd like to introduce python as an
>integral part of the development process for this concern, rather than them
>surrendering all to Microsoft.
>
>Thanks
>James Bourke
>
>
>
>
>
>
>------------------------------
>
>Message: 3
>Date: Sat, 29 Nov 2003 20:06:51 +1100
>From: JOHN KIRK <softscan at dodo.com.au>
>Subject: [Python-au] Newcastle Work Offered
>To: python-au at python.net
>Message-ID: <3FC861AB.8000909 at dodo.com.au>
>Content-Type: text/plain; charset=us-ascii; format=flowed
>
>Hi,
>I am based in Newcastle N.S.W. and need someone to teach me Python and
>oversee some game development I am doing.
>Anyone out there interested ?
>John
>
>
>
>
>
>
More information about the python-au
mailing list