[Zope] RESPONSE.write(text) and RESPONSE.flush()

Joe Goldthwaite joe at goldthwaites.com
Tue May 11 23:13:18 EDT 2004


Thanks Paul!  That did the trick.  Here's the object I ended up with;

class Build:

	def __init__(self, iOpts, RESPONSE):

		# iOpts is an object that stores user options from a database. It's needed
to build my standard menu header.
		self.iOpts = iOpts
		self.RESPONSE = RESPONSE

		# If I print one description on each line, they run off the bottom and the
user can't see the finish
		# statement.  As a work around, I'm going to display three descriptions on
each line separated by "... "
		# These are my counters to determine my line count and the number of bytes
written
		self.cnt = 0
		self.written = 0

		# Create an object that opens my database
		BC = BarsDB.BSConnection('P')

		# Tell the browser what kind of data is coming and how many bytes until
it's finished.
		RESPONSE.setHeader('Content-type', 'text/html')
		RESPONSE.setHeader('Content-length', 3000)

		# This sends my standard header with the company logo and title
		self.write(Html.MenuHeader(iOpts, "Database Rebuild"))

		# Now for the meat.  Loop through the database jobs sending a description
of what's happening to the
		# user and then execute the SQL statement
		cnt = 0
		for job in jobs:
			desc = job[0] + '... '

			cnt += 1
			if (cnt % 3) == 0:desc = job[0] + "<br>\n"

			self.write(desc)
			BC.cursor.execute(job[1])

		# We're finished so we can send the link to take us back home
		self.write('<br><br>\nDone! Click <a href="index_html">here</a> to return
to the home page.')

		# The finish function just sends enough spaces to finish out the 3000 we
told it at the beginning
		self.finish()

	def write(self, text):

		self.written += len(text)
		self.RESPONSE.write(text)

	def finish(self):

		self.write(' ' * (3000 - self.written))


There are probably better ways to doing things.  I still need to put some
error checking in so I can return descriptive messages to the user.  Right
now, if there's a problem, the page just never finishes.  That's ok for now
because the SQL statements have been well tested and seldom ever fail.  The
guy that will be running it is very sharp so he'll let me know if it doesn't
come back in a reasonable amount of time.

Thanks again Paul.  I owe you.

Joe Goldthwaite

-----Original Message-----
From: zope-bounces at zope.org [mailto:zope-bounces at zope.org]On Behalf Of
Paul Winkler
Sent: Tuesday, May 11, 2004 6:22 PM
To: zope at zope.org
Subject: Re: [Zope] RESPONSE.write(text) and RESPONSE.flush()


On Tue, May 11, 2004 at 05:31:08PM -0700, Joe Goldthwaite wrote:
> This works but not as well as I hoped.   There are so many jobs that the
> final result is off the bottom of the page so if the user doesn't scroll
> down, he can't see that its done.  I also wanted to display the data on a
> formatted HTML page but it looks like the write function encodes any html
so
> that you can see it.  This effectively keeps me from producing any kind of
> formatted output.  Is there any way to get the write function to return my
> text to the browser unchanged?

It does so. But maybe your browser thinks the result is
plain text, not html.  Maybe this would help (before any
calls to RESPONSE.write):

RESPONSE.setHeader('Content-type', 'text/html')

Note that also when you are doing RESPONSE.write, you should
always first do RESPONSE.setHeader('Content-length', N)
where N is the size of the data you will write.
If you don't know this length, you can overestimate it -
in which case the browser status bar will never show the page as "done".
I've dealt with the latter problem by keeping track of how
may bytes I write, and padding the result at the end, like this:

RESPONSE.write(' ' * (N - written_so_far))

Hacky, but it works fine.

> I also had the great idea of redirecting to a "finished" page with the
> command;
>
> RESPONSE.redirect("finished")
>
> Where "finished" is just a simple html page that displays "done" with a
link
> to return to the home page.  The only problem is that it doesn't work and
I
> don't know why.  I also tried to return a formatted html page from my
> external method after it calls the Build function but it's ignored.  For
> some reason, if I call RESPONSE.write(), I can't return any other html to
> Zope.

That's normal. RESPONSE.write() is a low-level way of getting data
to the client, and should not be combined with the "normal"
techniques.

--

Paul Winkler
http://www.slinkp.com

_______________________________________________
Zope maillist  -  Zope at zope.org
http://mail.zope.org/mailman/listinfo/zope
**   No cross posts or HTML encoding!  **
(Related lists -
 http://mail.zope.org/mailman/listinfo/zope-announce
 http://mail.zope.org/mailman/listinfo/zope-dev )




More information about the Zope mailing list