Re: Creating temperory files for a web application

2009-05-17 Thread sserrano
I would use a js plotting library, like http://code.google.com/p/flot/

On 8 mayo, 06:26, koranthala  wrote:
> Hi,
>    I am doing web development using Django. I need to create an image
> (chart) and show it to the users - based on some data which user
> selects.
>    My question is - how do I create a temporary image for the user? I
> thought of tempfile, but I think it will be deleted once the process
> is done - which would happen by the time user starts seeing the image.
> I can think of no other option other than to have another script which
> will delete all images based on time of creation.
>    Since python is extensively used for web development, I guess this
> should be an usual scenario for many people here. How do you usually
> handle this?

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Creating temperory files for a web application

2009-05-09 Thread Diez B. Roggisch


Hi Diez,
   I think I understood your point now.
   Is it ?
   (1) Have a separate URL for the image - update urls.py for that
   (2) Pass all the GET parameters to that URL again.
   (3) Recalculate the fields again in that URL
   (4) Create the image and send back as image/png based on the
received fields.
   Other than the fact that I will be hitting the DB twice - for the
fields in the original URL and for creating Image in the second URL, I
think this is the best option.
   Please let me know whether I have understood correctly.

   The same option can be done for CSV and PDF files too. Thank you
very much Diez.


Yep, that's what I meant.

Diez
--
http://mail.python.org/mailman/listinfo/python-list


Re: Creating temperory files for a web application

2009-05-08 Thread Stephen Hansen
>
>
> Thank you Diez.
> I would like to go ahead with the cleanest solution indeed.
> I am creating the image on the fly. But I could not understand what
> you meant by render to memory as a string.
> How do we send the image to the browser?
>
> Were you mentioning about having the image as a string and then
> passing to the browser based on data URL scheme  ?
> Or is it something else like XBM format or something? I am sorry - but
> I am quite unsure of what you meant?
>
> I am actually creating PDFs, CSVs and images on the fly. So, I need to
> delete them all after that.
> The images can be somewhat large - ~2MB. The RFC mentions that large
> data cannot be sent that way.
>

The question is, how are you generating these images? What you consider a
"file" and what a web server considers a "file" is really meaningless
actually. To a web browser, all a "file" is is a stream of content with a
certain content-type which it uses to determine how to treat it. This isn't
about the RFC2397. The web browser has no idea if the content its fetching
is from a file or a database or generated on the fly-- its all the same
thing. There's a URL which returns data, when requested. That data might be
a HTML file (as determined by its Content-type), in which case the browser
renders it. Or it might be a JPG, in which case it displays it. Or it might
be a zip file in which case it probably asks you where you want to save it.

Depending on how exactly you're actually "generating" the image, its quite
possible you don't need to use a real file at all. You can use a file-like
object, such as (c)StringIO and pass it to your image generation routines
which will generate the entire image in memory. You can write to this
file-like object as if it were a real file on the disk somewhere, seek,
scan, pass it to most libraries that need file objects and then when
done, you can return its contents... all without a -real- file on disk ever
being touched.

Like so vaguely:
f = cStringIO.StringIO()
my_library_which_makes_images(f)
f.seek(0)
return f.read()

Exactly how you indicate what the Content-type will be and do that will
depend on what web framework you're using.

For example, on one app I have -- I'm using TurboGears -- I do something
like this:

@expose(format="image/jpeg", content_type="image/jpeg")
def getJPEG(self, element_number):
element = self._resourceManager.getElement(element_number)
if element:
return element.data

return ''

Now the specifics of what's going on aren't important. But I'm connecting to
a remote system, and getting an object which contains the entire contents of
a JPEG in a single string. So the entire file is in memory. I could then
tweak or manipulate it with PIL to change that file all without any
filesystem access, working entirely on file-like objects in memory, then
return the contents.

--S
--
http://mail.python.org/mailman/listinfo/python-list


Re: Creating temperory files for a web application

2009-05-08 Thread koranthala
On May 8, 5:22 pm, koranthala  wrote:
> On May 8, 2:49 pm, "Diez B. Roggisch"  wrote:
>
>
>
> > koranthala wrote:
> > > Hi,
> > >    I am doing web development using Django. I need to create an image
> > > (chart) and show it to the users - based on some data which user
> > > selects.
> > >    My question is - how do I create a temporary image for the user? I
> > > thought of tempfile, but I think it will be deleted once the process
> > > is done - which would happen by the time user starts seeing the image.
>
> > What makes you think that? You are the one creating it, you are responsible
> > for deleting it.
>
> > > I can think of no other option other than to have another script which
> > > will delete all images based on time of creation.
> > >    Since python is extensively used for web development, I guess this
> > > should be an usual scenario for many people here. How do you usually
> > > handle this?
>
> > There are various solutions - tempfiles, files based on a criteria (e.g.
> > username and image-properties) so that they don't pollute the harddrive.
>
> > For both approaches cleaning up as you suggest might be needed.
>
> > Alternatively, if you use sessions, you can use memory-cached images.
>
> > Or you can use the database.
>
> > But the cleaning-up might get necessary here as well.
>
> > The cleanest solution would be if the image would be rendered on the fly,
> > based on GET-parameters (or REST-ful urls) so that you can render it into
> > memory as string, but then forget immediately about it.
>
> > Diez
>
> Thank you Diez.
> I would like to go ahead with the cleanest solution indeed.
> I am creating the image on the fly. But I could not understand what
> you meant by render to memory as a string.
> How do we send the image to the browser?
>
> Were you mentioning about having the image as a string and then
> passing to the browser based on data URL scheme  ?
> Or is it something else like XBM format or something? I am sorry - but
> I am quite unsure of what you meant?
>
> I am actually creating PDFs, CSVs and images on the fly. So, I need to
> delete them all after that.
> The images can be somewhat large - ~2MB. The RFC mentions that large
> data cannot be sent that way.
>
> Is it that I will have to delete the images using a separate script
> later?

Hi Diez,
   I think I understood your point now.
   Is it ?
   (1) Have a separate URL for the image - update urls.py for that
   (2) Pass all the GET parameters to that URL again.
   (3) Recalculate the fields again in that URL
   (4) Create the image and send back as image/png based on the
received fields.
   Other than the fact that I will be hitting the DB twice - for the
fields in the original URL and for creating Image in the second URL, I
think this is the best option.
   Please let me know whether I have understood correctly.

   The same option can be done for CSV and PDF files too. Thank you
very much Diez.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Creating temperory files for a web application

2009-05-08 Thread koranthala
On May 8, 2:49 pm, "Diez B. Roggisch"  wrote:
> koranthala wrote:
> > Hi,
> >    I am doing web development using Django. I need to create an image
> > (chart) and show it to the users - based on some data which user
> > selects.
> >    My question is - how do I create a temporary image for the user? I
> > thought of tempfile, but I think it will be deleted once the process
> > is done - which would happen by the time user starts seeing the image.
>
> What makes you think that? You are the one creating it, you are responsible
> for deleting it.
>
> > I can think of no other option other than to have another script which
> > will delete all images based on time of creation.
> >    Since python is extensively used for web development, I guess this
> > should be an usual scenario for many people here. How do you usually
> > handle this?
>
> There are various solutions - tempfiles, files based on a criteria (e.g.
> username and image-properties) so that they don't pollute the harddrive.
>
> For both approaches cleaning up as you suggest might be needed.
>
> Alternatively, if you use sessions, you can use memory-cached images.
>
> Or you can use the database.
>
> But the cleaning-up might get necessary here as well.
>
> The cleanest solution would be if the image would be rendered on the fly,
> based on GET-parameters (or REST-ful urls) so that you can render it into
> memory as string, but then forget immediately about it.
>
> Diez

Thank you Diez.
I would like to go ahead with the cleanest solution indeed.
I am creating the image on the fly. But I could not understand what
you meant by render to memory as a string.
How do we send the image to the browser?

Were you mentioning about having the image as a string and then
passing to the browser based on data URL scheme  ?
Or is it something else like XBM format or something? I am sorry - but
I am quite unsure of what you meant?

I am actually creating PDFs, CSVs and images on the fly. So, I need to
delete them all after that.
The images can be somewhat large - ~2MB. The RFC mentions that large
data cannot be sent that way.

Is it that I will have to delete the images using a separate script
later?

--
http://mail.python.org/mailman/listinfo/python-list


Re: Creating temperory files for a web application

2009-05-08 Thread Diez B. Roggisch
koranthala wrote:

> Hi,
>I am doing web development using Django. I need to create an image
> (chart) and show it to the users - based on some data which user
> selects.
>My question is - how do I create a temporary image for the user? I
> thought of tempfile, but I think it will be deleted once the process
> is done - which would happen by the time user starts seeing the image.

What makes you think that? You are the one creating it, you are responsible
for deleting it.

> I can think of no other option other than to have another script which
> will delete all images based on time of creation.
>Since python is extensively used for web development, I guess this
> should be an usual scenario for many people here. How do you usually
> handle this?

There are various solutions - tempfiles, files based on a criteria (e.g.
username and image-properties) so that they don't pollute the harddrive.

For both approaches cleaning up as you suggest might be needed.

Alternatively, if you use sessions, you can use memory-cached images.

Or you can use the database.

But the cleaning-up might get necessary here as well.

The cleanest solution would be if the image would be rendered on the fly,
based on GET-parameters (or REST-ful urls) so that you can render it into
memory as string, but then forget immediately about it.

Diez
--
http://mail.python.org/mailman/listinfo/python-list


Creating temperory files for a web application

2009-05-08 Thread koranthala
Hi,
   I am doing web development using Django. I need to create an image
(chart) and show it to the users - based on some data which user
selects.
   My question is - how do I create a temporary image for the user? I
thought of tempfile, but I think it will be deleted once the process
is done - which would happen by the time user starts seeing the image.
I can think of no other option other than to have another script which
will delete all images based on time of creation.
   Since python is extensively used for web development, I guess this
should be an usual scenario for many people here. How do you usually
handle this?
--
http://mail.python.org/mailman/listinfo/python-list