ASE Home Page Products Download Purchase Support About ASE
ChartDirector Support
Forum HomeForum Home   SearchSearch

Message ListMessage List     Post MessagePost Message

  Python CGI to WSGI
Posted by Rudy Brasser on Jan-14-2014 19:24
Hello,

How can I change the CGI version of a simple graph like this (for example):

  from pychartdir import *

  data = [85, 156, 179.5, 211, 123]
  labels = ["Mon", "Tue", "Wed", "Thu", "Fri"]
  c = XYChart(250, 250)
  c.setPlotArea(30, 20, 200, 200)
  c.addBarLayer(data)
  c.xAxis().setLabels(labels)

  # Output the chart
  print("Content-type: image/png\\n")
  binaryPrint(c.makeChart2(PNG))

To make it display the graph (in the browser) using as a WSGI application; like this
example:

  def application(environ, start_response):
    status = '200 OK'
    output = """
                <html>
                  <head><title>My Page</title></head>
                  <body>
                    <img border="0" src="Static/IMG/graph.png"/>
                  </body>
               </html>
    """
  response_headers = [('Content-type', 'text/html'),
                      ('Content-length', str(len(output)))]
  start_response(status, response_headers)

  return [output]

Or:  how do I change the "binaryPrint" thing into an "output" object (if that's even
possible)?

Thanks in advance,

Rudy Brasser

  Re: Python CGI to WSGI
Posted by Peter Kwan on Jan-15-2014 06:09
Hi Ruby,

I have never used SWGI before, but based on your example, the:

   print("Content-type: image/png\\n")
   binaryPrint(c.makeChart2(PNG))

probably translates to SWGI as:

    output = c.makeChart2(PNG)

    response_headers = [('Content-type', 'image/png'),
                      ('Content-length', str(len(output)))]
    start_response(status, response_headers)
    return [output]

Note that I assume SWGI does support non-text output (a chart is an image, not text). On
Windows, the Python "print" statement only supports text. It will change UNIX style linefeed
character to Windows style linefeed. This is fine for text, but would corrupt the image.
That's why we have "binaryPrint". If SWGI can support non-text output, then just returning
the image should be fine.

Regards
Peter Kwan

  Re: Python CGI to WSGI
Posted by Rudy Brasser on Jan-21-2014 14:23
Hello Peter,

Thanks for your excellent reply!
(My knowledge of O.O.P. obviously is not good enough)

It worked; Here's the code that eventually did it, with html & image Response Headers
(HTML & Image Content Types), in case other people want to know:

Thanks,

Rudy
---------------------------------------------------------------------------
Apache 2.4 httpd.conf:

WSGIScriptAlias /htmlpage "C:/Data/wsgi_app/htmlpage.wsgi"
WSGIScriptAlias /graph    "C:/Data/wsgi_app/graph.wsgi"

<Directory "C:/Data/wsgi_app">
  AllowOverride none
  Options none
  Order deny,allow
  Require all granted
</Directory>

---------------------------------------------------------------------------
htmlpage.wsgi:

def application(environ, start_response):

  status = '200 OK'

  output = """<html>
                   <body>
                     <img border="0" src="http://localhost/graph" alt="Graph" />
                   </body>
                </html>  """

  response_headers = [('Content-type', 'text/html'),
                              ('Content-length', str(len(output)))]
  start_response(status, response_headers)

  return [output]

---------------------------------------------------------------------------
graph.wsgi:

from pychartdir import *

def application(environ, start_response):

  data = [25, 18, 15, 12, 8, 30, 35]
  labels = ["Labor", "Licenses", "Taxes", "Legal", "Insurance", "Facilities","Production"]
  c = PieChart(360, 300)
  c.setPieSize(180, 140, 100)
  c.setData(data, labels)
  output = c.makeChart2(PNG)

  status = '200 OK'
  response_headers = [('Content-type', 'image/png'),
                              ('Content-length', str(len(output)))]
  start_response(status, response_headers)

  return [output]

---------------------------------------------------------------------------
URL: http://localhost/htmlpage
---------------------------------------------------------------------------