open an html string in a browser?

Kevin Altis altis at semi-retired.com
Sat Jan 18 15:20:21 EST 2003


I don't think you have any choice but to create an actual file for the
browser to read if you want to use the webbrowser module; the browser will
know nothing of zip files. If you want to use the actual name of the file,
but use the temporary directory on your system, make a call to
tempfile.mktemp() and then you can use the tempfile.tempdir variable for
creating the files. Something like:

import tempfile
tempfile.mktemp()
path = os.path.join(tempfile.tempdir, 'myfile.html')

Then just write the file contents before you call webbrowser with the path
you created.

http://www.python.org/doc/lib/module-tempfile.html

Note that the documenation is a bit out-of-date for Python 2.2.2, the docs
for 2.3a1 seem to better reflect what the module actually does:

http://www.python.org/doc/2.3a1/lib/module-tempfile.html

As an alternative to using the webbrowser module, you could use your own GUI
to display the HTML in which case you don't have to write out a temporary
file. wxPython has a wxHtmlWindow control which is cross-platform as well as
wrappers for the Windows IE control, which of course only works on Windows.
PythonCard has samples for using both.

http://pythoncard.sourceforge.net/samples/simpleBrowser.html

http://pythoncard.sourceforge.net/samples/simpleIEBrowser.html

Since it is possible to set the page by providing both a URL or text, it is
possible to read the file from the zip and then display that content
directly without using an intermediate file. I just created a zip containing
an HTML file to test this with the simpleIEBrowser sample, run with the -s
(Shell) command-line option:

>>> import zipfile
>>> f = zipfile.ZipFile('index-in-zip.zip')
>>> f.namelist()
['index-in-zip.html']
>>> data = f.read('index-in-zip.html')
>>> f.close()
>>> comp.htmlDisplay.text = data

The last line is referencing the html component and its text attribute and
works as expected.

Finally, if you are on Windows, then you could also access the browser
directly using win32com (part of Mark Hammond's win32all package) and set
its content directly, but I don't have an example of that offhand.

ka

"Lance" <lbrannma at cablespeed.com> wrote in message
news:v2j7tdai7bk9a9 at corp.supernews.com...
> Hi All,
>
> I have a zip file containing perhaps 800 html files. I want to use Python
to
> open one of these files in a browser.
> Suppose the filename is myfile.html.
>
> I've tried webbrowser and zipfile. These don't work.
>
> The reason is that zipfile.read('myfile.html') returns a string,
containing
> the contents of the file. However, Webbrowser requires a URL (in this case
> the name of an html file).
>
> I don't want to save the string from zipfile.read to disk.
>
> Any suggestions will be appreciated.
>
> Thanks,
> Lance
>
>






More information about the Python-list mailing list