Passing data to/from Python
Dear Zopistas, I am in the process of learning about Zope, and trying to determine whether to make the switch from pure Python CGI scripts. If someone could answer the following questions (and I apologize, as it will probably seem naive), it would really help. Let's suppose I have a page where the user queries a data base, for instance a telephone directory. The telephone directory is in an Excel file. At present, I have a CGI script that opens an ODBC.Windows connection, executes an SQL query, and outputs the results (names, phone numbers) as an HTML table using "print". What is the best way to do this in Zope? Here are the points that I don't get: 1. It seems that the only way to establish a DB connection is through Gadfly. There are warnings everywhere that Gadfly is inefficient. Therefore, I want to use ODBC (ODBC.Windows), like I did before. How? 2. I might eventually need to do complex calculations on the retrieved data, so I would prefer using Python code in an external method, rather than a DTML method. In order to get Zope to accept the line "import ODBC.Windows", I had to copy the ODBC and DateTime directories from my regular Python installation (PYTHON/lib) to ZOPE/bin/lib. But then, when I try running the script, I get one of the following problems: a. If I go through authentification, the script runs, but I cannot establish the connection to the database. b. If I type in the URL of my search page directly, without authentification, I get a Zope error: "initialization of module mxODBC failed (exceptions.AttributeError:mxDateTimeAPI)". Is my ODBC.Windows installation wrong, or do I have a more fundamental problem? 3. What if my Python function in my external method wants to return information to my DTML method? For instance, how do I return a Python list, and what do I do in my DTML method to process it (e.g., output a line for every member of the list)? 4. The way I call my Python function now is through the <form method="POST" action="search"> tag, where "search" is the external method ID. The form has a field called "name", and my Python function has an argument also called "name", and so it recuperates the string that was entered in the "name" field. Can I pass the "name" as an argument to a DTML method? If these questions have answers, I would really appreciate them. Otherwise, if I'm completely off-base (for instance, if I'm thinking in Python terms and not in Zope terms), please point me in the right direction! Thanks in advance. Mark Wexler
Mark Wexler wrote:
Dear Zopistas,
I am in the process of learning about Zope, and trying to determine whether to make the switch from pure Python CGI scripts. If someone could answer the following questions (and I apologize, as it will probably seem naive), it would really help.
Let's suppose I have a page where the user queries a data base, for instance a telephone directory. The telephone directory is in an Excel file. At present, I have a CGI script that opens an ODBC.Windows connection, executes an SQL query, and outputs the results (names, phone numbers) as an HTML table using "print".
What is the best way to do this in Zope? Here are the points that I don't get:
1. It seems that the only way to establish a DB connection is through Gadfly. There are warnings everywhere that Gadfly is inefficient. Therefore, I want to use ODBC (ODBC.Windows), like I did before. How?
There's an ODBC database adapter for Windows machines. It does not work under UNIX. The Sybase DA works against MS SQL Server 6.5 and below from UNIX. Look under "Downloads" on Zope.org. Also read the ZSQL User Guide.
2. I might eventually need to do complex calculations on the retrieved data, so I would prefer using Python code in an external method, rather than a DTML method. In order to get Zope to accept the line "import ODBC.Windows", I had to copy the ODBC and DateTime directories from my regular Python installation (PYTHON/lib) to ZOPE/bin/lib. But then, when I try running the script, I get one of the following problems:
a. If I go through authentification, the script runs, but I cannot establish the connection to the database.
b. If I type in the URL of my search page directly, without authentification, I get a Zope error: "initialization of module mxODBC failed (exceptions.AttributeError:mxDateTimeAPI)".
Is my ODBC.Windows installation wrong, or do I have a more fundamental problem?
Hmm. I've never seen ODBC.Windows. We've punted on supporting ODBC under UNIX, and we're now supposed to be contracting out to some other folks to provide it for us. Not sure when this will happen. You can try to write your own ODBC DA for UNIX/Linux. There is a HowTo on writing a DA someplace out there that Chris Petrilli wrote.
3. What if my Python function in my external method wants to return information to my DTML method? For instance, how do I return a Python list, and what do I do in my DTML method to process it (e.g., output a line for every member of the list)?
external method named "pyfunction": def pyfunction(self): return ['1','2','3'] a dtml method that uses pyfunction: <dtml-in pyfunction> <dtml-var sequence-item><br> </dtml-in> Should output: 1 2 3
4. The way I call my Python function now is through the <form method="POST" action="search"> tag, where "search" is the external method ID. The form has a field called "name", and my Python function has an argument also called "name", and so it recuperates the string that was entered in the "name" field. Can I pass the "name" as an argument to a DTML method?
If you use an external method to handle the form input, you should be able to get at its attributes by looking in REQUEST ala: def myformhandler(self, REQUEST): a = REQUEST['a'] b = REQUEST['b'] where 'a' and 'b' are form field names. This can also be done by: def myformhandler(self): a = self.REQUEST['a'] b = self.REQUEST['b']
If these questions have answers, I would really appreciate them. Otherwise, if I'm completely off-base (for instance, if I'm thinking in Python terms and not in Zope terms), please point me in the right direction!
Thanks in advance. Mark Wexler
_______________________________________________ Zope maillist - Zope@zope.org http://lists.zope.org/mailman/listinfo/zope ** No cross posts or HTML encoding! ** (Related lists - http://lists.zope.org/mailman/listinfo/zope-announce http://lists.zope.org/mailman/listinfo/zope-dev )
-- Chris McDonough Digital Creations Publishers of Zope - http://www.zope.org
process it (e.g., output a line for every member of the list)?
external method named "pyfunction":
def pyfunction(self): return ['1','2','3']
a dtml method that uses pyfunction:
Mind if I ask a follow up question to that? What if I wanted to return a list of hashes, how would I address the keys in the hashes?
1. It seems that the only way to establish a DB connection is through Gadfly. There are warnings everywhere that Gadfly is inefficient. Therefore, I want to use ODBC (ODBC.Windows), like I did before. How?
First of all, someone needs to let you know that data should never be stored in an Excel file. Anyway, what you would do is use the Zope ODBC database adapter. The Gadfly one is the only one installed by default, but others are availible at zope.org You really should be storing that in a relational database though. Speaking for years of database experience, Excel is never a good place to keep things.
2. I might eventually need to do complex calculations on the retrieved data, so I would prefer using Python code in an external method, rather than a DTML method. In order to get Zope to accept the line "import ODBC.Windows", I had to copy the ODBC and DateTime directories from my regular Python installation (PYTHON/lib) to ZOPE/bin/lib. But then, when I try running the script, I get one of the following problems:
a. If I go through authentification, the script runs, but I cannot establish the connection to the database.
This isn't really a Zope issue. It's a bug in your script or the serveor or something to that effect.
b. If I type in the URL of my search page directly, without authentification, I get a Zope error: "initialization of module mxODBC failed (exceptions.AttributeError:mxDateTimeAPI)".
Is my ODBC.Windows installation wrong, or do I have a more fundamental problem?
Ask those in the ODBC.Windows mailing list; this is not a Zope issue. You might consider though, not using so many external methods and whether writing packages - they are more powerful and can be more powerful. External Methods are usually for quick one-timers.
3. What if my Python function in my external method wants to return information to my DTML method? For instance, how do I return a Python list, and what do I do in my DTML method to process it (e.g., output a line for every member of the list)?
I beleive you can do just that - return it as a list, and use it like this inside DTML <dtml-in externalmethodid> ... </dtml-in>
4. The way I call my Python function now is through the > tag, where "search" is the external method ID. The form has a field called "name", and my Python function has an argument also called "name", and so it recuperates the string that was entered in the "name" field. Can I pass the "name" as an argument to a DTML method
I'll have to pass on this question to someone who is more experienced.
If these questions have answers, I would really appreciate them. Otherwise, if I'm completely off-base (for instance, if I'm thinking in Python terms and not in Zope terms), please point me in the right direction!
Hang in there. Learning to do this kind of thing in ZOPE is hard - I know because I'm trying to do it myself.
4. The way I call my Python function now is through the > tag, where "search" is the external method ID. The form has a field called "name", and my Python function has an argument also called "name", and so it recuperates the string that was entered in the "name" field. Can I pass the "name" as an argument to a DTML method
I'll have to pass on this question to someone who is more experienced.
<dtml-var "search(name=name)"> should do the job, if I understand your question right. (Works like in Python.) And remember: If you want performance, use any OS other than Windoze. Two good free DBs are PostGres (currently in 7.0 beta 3, yeah!) and MySQL. If you go to the RedHat site and sign up, they will send you Oracle 8i for free (or do they not do that anymore). I think Sybase has a similar promotion program. For all these DBs you have Zope DB Adapters. Zope is cool, the docs are a big issue (I know), but a lot of people work very hard to change that. If you have suggestions of how to improve them, mail: Zope Documentation Project <zdp@zope.org>. Thanks. Regards, Stephan -- Stephan Richter - (901) 573-3308 - srichter@cbu.edu CBU - Physics & Chemistry; Framework Web - Web Design & Development PGP Key: 735E C61E 5C64 F430 4F9C 798E DCA2 07E3 E42B 5391
On Sun, 26 Mar 2000, you wrote:
1. It seems that the only way to establish a DB connection is through Gadfly. There are warnings everywhere that Gadfly is inefficient. Therefore, I want to use ODBC (ODBC.Windows), like I did before. How?
Gadfly is just one of the db's you can use. The aquaduct product family gives you many options look at http://www.zope.org/Products/DA/ZODBCDA for the ODBC adapter. This is defenitely the easier way
2. I might eventually need to do complex calculations on the retrieved data, so I would prefer using Python code in an external method,
You can do quite a bit with dtml unless of course you prefer python and sometimes it is easier to do stuff in python, dtml is not a replacement by any means
3. What if my Python function in my external method wants to return information to my DTML method? For instance, how do I return a Python list, and what do I do in my DTML method to process it (e.g., output a line for every member of the list)?
return a list and process it using <dtml-in > which behaves very similar to a database cursor
Can I pass the "name" as an argument to a DTML method? Absolutely with the dtml programming you can do the same things and more All the form variables show up in the REQUEST object and become part of the namespace in your script,you can use them like any other variable in the script -- ########################## necessity is the mother of invention ##########################
On Mon, 27 Mar 2000, Mark Wexler wrote:
Dear Zopistas,
I am in the process of learning about Zope, and trying to determine whether to make the switch from pure Python CGI scripts. If someone could answer the following questions (and I apologize, as it will probably seem naive), it would really help.
We all had to start somewhere.. (o8
Let's suppose I have a page where the user queries a data base, for instance a telephone directory. The telephone directory is in an Excel file. At present, I have a CGI script that opens an ODBC.Windows connection, executes an SQL query, and outputs the results (names, phone numbers) as an HTML table using "print".
Sounds simple enough...
What is the best way to do this in Zope? Here are the points that I don't get:
1. It seems that the only way to establish a DB connection is through Gadfly. There are warnings everywhere that Gadfly is inefficient. Therefore, I want to use ODBC (ODBC.Windows), like I did before. How?
Nonono.... ZOPE uses the Gadfly db to store "zope objects". It can also connect to other DBs, and access them via SQL methods. There are a number of DB interfaces, including an ODBC interface.
2. I might eventually need to do complex calculations on the retrieved data, so I would prefer using Python code in an external method, rather than a DTML method. In order to get Zope to accept the line "import ODBC.Windows", I had to copy the ODBC and DateTime directories from my regular Python installation (PYTHON/lib) to ZOPE/bin/lib. But then, when I try running the script, I get one of the following problems:
a. If I go through authentification, the script runs, but I cannot establish the connection to the database.
b. If I type in the URL of my search page directly, without authentification, I get a Zope error: "initialization of module mxODBC failed (exceptions.AttributeError:mxDateTimeAPI)".
Is my ODBC.Windows installation wrong, or do I have a more fundamental problem?
Erm.. i'm out of my depth on this one.... i'll leave that to someone else. (o8
3. What if my Python function in my external method wants to return information to my DTML method? For instance, how do I return a Python list, and what do I do in my DTML method to process it (e.g., output a line for every member of the list)?
afaik, you can simply return a list. As for iterating through the list, you would use <dtml-in myMethod> stuff goes here </dtml-in>
4. The way I call my Python function now is through the <form method="POST" action="search"> tag, where "search" is the external method ID. The form has a field called "name", and my Python function has an argument also called "name", and so it recuperates the string that was entered in the "name" field. Can I pass the "name" as an argument to a DTML method?
All fields passed in a POST are available via the REQUEST.form map. Also, so long as they don't match the name of other objects in the namespace, you can access then directly. So, your 'name' field will be accessible: <dtml-var name> <-- assuming you don't have any other objects called "name" <dtml-var "REQUEST['name']"> or <dtml-var "REQUEST.form['name']">
If these questions have answers, I would really appreciate them. Otherwise, if I'm completely off-base (for instance, if I'm thinking in Python terms and not in Zope terms), please point me in the right direction!
Here are my answers, I hope they help. I'm sure others will contribute comments, but feel free to grill me for further details.
Thanks in advance. Mark Wexler
-- Have a better one, Curtis. <dtml-var standard_work_disclaimer>
Hey Mark, This is your lucky day... Zope does this without even blinking... I would say (and without starting a flame war) that Zope's ability to easily connect to databases is one its best features. To the questions... 1) Gadfly is the example. You connect to anything that there is a ZSQL connection product for (Oracle, Sybase?, PostGreSQL, MySQL, Gadfly) 2) You get back the data and can do anything you want with it. You can parse it, calculate it (although you might want to look into doing some of it in SQL) or just spit it out. Authentication has nothing to do with what you do with the data. 3) It is easy in DTML, just do a <dtml-in> to go over the list. 4) Yup. You build a form and send that form to a DTML method that processes the form. If you are super slick you have it in the same DTML method. Don't worry about thinking in Python terms. I came from Python.. thought Zope was going to be easy, found out it hard than studied up a bit and now respect it. There are a LOT of things you can do in Zope, most of them "like" things you do in Python, just stated a bit different. The DTML reference guide is your buddy. You read that and come back here when you are truly stuck. Now go Zope... :) J
participants (7)
-
Chris McDonough -
Curtis Maloney -
J. Atwood -
Ken Kinder -
Mark Wexler -
sathya -
Stephan Richter