List Info

Thread: Getting contents of a Http Response Object




Getting contents of a Http Response Object
country flaguser name
United States
2007-10-19 06:32:21
I'm creating a pdf document dynamically, I need to include
barchart
data in the document. The  bar chart data is also created
dynamically
with report lab. I can get the barchart data at my url ...

def barchart_interests(request):

.............


    #get a GIF (or PNG, JPG, or
whatever)
    binaryStuff = d.asString('gif')

    return HttpResponse(binaryStuff, 'image/gif')

Now I want to use that barchart in my pdf ...

def dyn_pdf(request):

   buffer = StringIO()

    # Create the PDF object, using the StringIO object as
its
"file."
    p = canvas.Canvas(buffer)

    # Get the HttpResponseObject
    image = interests_barchart(request)

    # Insert the image into the pdf document
    p.drawInlineImage(image, 100,350, width=100,height=100)

    p.showPage()
    p.save()

Things that I've tried ....

       p.drawInlineImage(image._get_contents, 100,350,
width=100,height=100)
       Attribute Error: 'function' object has no attribute
'format'

       p.drawInlineImage(image.content, 100,350,
width=100,height=100)
       I/O Error: Cannot open resource "GIF87a

I know that PHP has a function called
get_file_from_contents() or
something similar to that. I'm not entirely sure what I
should be
doing here to get the .gif image from the
HttpResponseObject. I can
access the barchart no problem with my browser

Thanks


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "Django users" group.
To post to this group, send email to django-usersgooglegroups.com
To unsubscribe from this group, send email to
django-users-unsubscribegooglegroups.com
For more options, visit this group at htt
p://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---


Re: Getting contents of a Http Response Object
country flaguser name
Australia
2007-10-19 06:55:37
On Fri, 2007-10-19 at 04:32 -0700, dbee wrote:
> I'm creating a pdf document dynamically, I need to
include barchart
> data in the document. The  bar chart data is also
created dynamically
> with report lab. I can get the barchart data at my url
...
> 
> def barchart_interests(request):
> 
> .............
> 
> 
>     #get a GIF (or PNG, JPG, or
> whatever)
>     binaryStuff = d.asString('gif')
> 
>     return HttpResponse(binaryStuff, 'image/gif')
> 
> Now I want to use that barchart in my pdf ...
> 
> def dyn_pdf(request):
> 
>    buffer = StringIO()
> 
>     # Create the PDF object, using the StringIO object
as its
> "file."
>     p = canvas.Canvas(buffer)
> 
>     # Get the HttpResponseObject
>     image = interests_barchart(request)
> 
>     # Insert the image into the pdf document
>     p.drawInlineImage(image, 100,350,
width=100,height=100)
> 
>     p.showPage()
>     p.save()
> 
> Things that I've tried ....
> 
>        p.drawInlineImage(image._get_contents, 100,350,
> width=100,height=100)
>        Attribute Error: 'function' object has no
attribute 'format'
> 
>        p.drawInlineImage(image.content, 100,350,
width=100,height=100)
>        I/O Error: Cannot open resource "GIF87a

This one is close, but you're not reading the Reportlab
documentation
correctly. drawInlineImage() takes a filename or a PIL image
object as
its first argument, where as image.content is the binary
data that is in
the HttpResponse. So you need to create an Image object from
that data
first. This should be close:

        from PIL import Image
        from cStringIO import StringIO
        
        ...
        img = Image.open(StringIO(image.content))
        p.drawInlineImage(img, ...)
        
Read the PIL documentation for more possibilities.

Regards,
Malcolm


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "Django users" group.
To post to this group, send email to django-usersgooglegroups.com
To unsubscribe from this group, send email to
django-users-unsubscribegooglegroups.com
For more options, visit this group at htt
p://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---


Re: Getting contents of a Http Response Object
country flaguser name
United States
2007-10-19 08:33:42
Hi Matthew,

Thanks. The problem is though, that I'm following the docs
and using
another class to 'shape' my barchart ...

# Use this one for client
signup
class interests_class(Drawing):

    format = 'gif'

    def __init__(self, width=400, height=250, *args, **kw):

        apply(Drawing.__init__,(self,width,height)+args,kw)

        # set the chart
type
        self.add(VerticalBarChart(), name='chart')

        #
Title
        self.add(String(200,240,''), name='title')

        ...........

if __name__=='__main__':
    #use the standard 'save' method to save barchart.gif,
barchart.pdf
etc
    #for quick feedback while
working.
 
interests_class().save(formats=['gif','png','jpg','pdf'],out
Dir='.',fnRoot='barchart')

# Then I'm using the above class to create my barchart image
....

def interests_barchart(request):

    d = interests_class()

    d.chart.data=[ ....  ]

    #  Give it a
title
    if request.has_key('title'):
        d.title.text = request['title']

    # get a GIF (or PNG, JPG, or
whatever)
    # binaryStuff = d.asString('gif')

    return d

# I'd already tried your suggestion previously as in ...

## Takes the d object here
image = interests_barchart(request)

p.drawInlineImage(image, 100,350, width=100,height=100)

The problem is that the d object isn't actually a PIL object
at all.
The barchart_interests function works with a Drawing PIL
object, but
it doesn't inherit from it and in turn - it doesn't return a
PIL
object. 'd' is a barchart_interests() object, it has no
format
attribute and no convert() method. I can fake the format
attribute but
I can't fake the convert method.

Strictly speaking then, d object should inherit from Drawing
-
although the doc examples do it my way and not the other
way. I'd
prefer not to have to rewrite the class.

The preferable solution would be to be able to
get_file_contents()
from the HttpResponseObject somehow. Otherwise I'll have to
write
multiple classes to output my barcharts in multiple ways.
(This isn't
my only barchart).

Thanks

On Oct 19, 12:55 pm, Malcolm Tredinnick <malc...pointy-stick.com>
wrote:
> On Fri, 2007-10-19 at 04:32 -0700, dbee wrote:
> > I'm creating a pdf document dynamically, I need to
include barchart
> > data in the document. The  bar chart data is also
created dynamically
> > with report lab. I can get the barchart data at my
url ...
>
> > def barchart_interests(request):
>
> > .............
>
> >     #get a GIF (or PNG, JPG, or
> > whatever)
> >     binaryStuff = d.asString('gif')
>
> >     return HttpResponse(binaryStuff, 'image/gif')
>
> > Now I want to use that barchart in my pdf ...
>
> > def dyn_pdf(request):
>
> >    buffer = StringIO()
>
> >     # Create the PDF object, using the StringIO
object as its
> > "file."
> >     p = canvas.Canvas(buffer)
>
> >     # Get the HttpResponseObject
> >     image = interests_barchart(request)
>
> >     # Insert the image into the pdf document
> >     p.drawInlineImage(image, 100,350,
width=100,height=100)
>
> >     p.showPage()
> >     p.save()
>
> > Things that I've tried ....
>
> >        p.drawInlineImage(image._get_contents,
100,350,
> > width=100,height=100)
> >        Attribute Error: 'function' object has no
attribute 'format'
>
> >        p.drawInlineImage(image.content, 100,350,
width=100,height=100)
> >        I/O Error: Cannot open resource
"GIF87a
>
> This one is close, but you're not reading the Reportlab
documentation
> correctly. drawInlineImage() takes a filename or a PIL
image object as
> its first argument, where as image.content is the
binary data that is in
> the HttpResponse. So you need to create an Image object
from that data
> first. This should be close:
>
>         from PIL import Image
>         from cStringIO import StringIO
>
>         ...
>         img = Image.open(StringIO(image.content))
>         p.drawInlineImage(img, ...)
>
> Read the PIL documentation for more possibilities.
>
> Regards,
> Malcolm


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "Django users" group.
To post to this group, send email to django-usersgooglegroups.com
To unsubscribe from this group, send email to
django-users-unsubscribegooglegroups.com
For more options, visit this group at htt
p://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---


Re: Getting contents of a Http Response Object
country flaguser name
United States
2007-10-19 08:41:44
Aha, thanks dude 

On Oct 19, 12:55 pm, Malcolm Tredinnick <malc...pointy-stick.com>
wrote:
> On Fri, 2007-10-19 at 04:32 -0700, dbee wrote:
> > I'm creating a pdf document dynamically, I need to
include barchart
> > data in the document. The  bar chart data is also
created dynamically
> > with report lab. I can get the barchart data at my
url ...
>
> > def barchart_interests(request):
>
> > .............
>
> >     #get a GIF (or PNG, JPG, or
> > whatever)
> >     binaryStuff = d.asString('gif')
>
> >     return HttpResponse(binaryStuff, 'image/gif')
>
> > Now I want to use that barchart in my pdf ...
>
> > def dyn_pdf(request):
>
> >    buffer = StringIO()
>
> >     # Create the PDF object, using the StringIO
object as its
> > "file."
> >     p = canvas.Canvas(buffer)
>
> >     # Get the HttpResponseObject
> >     image = interests_barchart(request)
>
> >     # Insert the image into the pdf document
> >     p.drawInlineImage(image, 100,350,
width=100,height=100)
>
> >     p.showPage()
> >     p.save()
>
> > Things that I've tried ....
>
> >        p.drawInlineImage(image._get_contents,
100,350,
> > width=100,height=100)
> >        Attribute Error: 'function' object has no
attribute 'format'
>
> >        p.drawInlineImage(image.content, 100,350,
width=100,height=100)
> >        I/O Error: Cannot open resource
"GIF87a
>
> This one is close, but you're not reading the Reportlab
documentation
> correctly. drawInlineImage() takes a filename or a PIL
image object as
> its first argument, where as image.content is the
binary data that is in
> the HttpResponse. So you need to create an Image object
from that data
> first. This should be close:
>
>         from PIL import Image
>         from cStringIO import StringIO
>
>         ...
>         img = Image.open(StringIO(image.content))
>         p.drawInlineImage(img, ...)
>
> Read the PIL documentation for more possibilities.
>
> Regards,
> Malcolm


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "Django users" group.
To post to this group, send email to django-usersgooglegroups.com
To unsubscribe from this group, send email to
django-users-unsubscribegooglegroups.com
For more options, visit this group at htt
p://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---


[1-4]

about | contact  Other archives ( Real Estate discussion Medical topics )