Question

I´m having trouble in creating a file and export to .yaml. I´m using Google App Engine with Python 2.5. Don´t understand the Yaml doc´s, it makes me confused.

What i want is to create a file and save it. It´s necessary to get entities from Models.

class SaveYAML(webapp.RequestHandler):
    def post(self):
        user = db.Query(models.User)
        user = user.filter('user =', users.get_current_user())
        users = user.fetch(limit = 1)

        for user in users:
            print(user.name, user.adress, user.phone, user.city)

        self.response.headers['Content-Type'] = 'application/yaml'
        self.response.headers['Content-Disposition'] = 'filename = myYaml.yaml' 

With this snippet i can view in a browser, when i click in a button the information retrieved from models. Maybe it´s because of print method, but it doesn´t create a file But when i upload my app to Google App Engine it doesn´t show the same info. It shows only 'Status 200 Ok'.

Can someone point me in the right direction? Do i have to import pyyaml library?

I changed some code to:

print(yaml.dump(user, sys.stdout))

and the result was this in a browser:

- !!binary |
  /9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAgGBgcGBQgHBwcJCQgKDBQNDAsLDBkSEw8UHRofHh0a
  HBwgJC4nICIsIxwcKDcpLDAxNDQ0Hyc5PTgyPC4zNDL/2wBDAQkJCQwLDBgNDRgyIRwhMjIyMjIy
  MjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjL/wAARCAAnACgDASIA
  AhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUFBAQA
  AAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkKFhcYGRolJicoKSo0NTY3
  ODk6Q0RFRkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1dnd4eXqDhIWGh4iJipKTlJWWl5iZmqKjpKWm
  p6ipqrKztLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2uHi4+Tl5ufo6erx8vP09fb3+Pn6/8QAHwEA
  AwEBAQEBAQEBAQAAAAAAAAECAwQFBgcICQoL/8QAtREAAgECBAQDBAcFBAQAAQJ3AAECAxEEBSEx
  BhJBUQdhcRMiMoEIFEKRobHBCSMzUvAVYnLRChYkNOEl8RcYGRomJygpKjU2Nzg5OkNERUZHSElK
  U1RVVldYWVpjZGVmZ2hpanN0dXZ3eHl6goOEhYaHiImKkpOUlZaXmJmaoqOkpaanqKmqsrO0tba3
  uLm6wsPExcbHyMnK0tPU1dbX2Nna4uPk5ebn6Onq8vP09fb3+Pn6/9oADAMBAAIRAxEAPwD2WxvX
  h8zzNxhEjgk87fmPI9R6+lbKsGUMpBBGQR3rAhKxRTyOyqglck9Mc1nX/iVdGb7JZr9qnlBMcCc7
  T3Psvqeg7elAG7rmv6f4esGu7+ZUXoq5+Zz6Cuc8I+ItS8X6pLqK/wCiaXbKYhbjkyuRnJOOw7Cs
  jXvC8epW0Ta5IdR1O7dTFDG5Eaeirj1xy3oG6dK7rQdGtvDuiQ2FuoCRLl2A+8x5J/OtFKKhtqZu
  MnPfQoajeXDeLtMtICfLG55cNj5Qp6/iVoqLQAb/AMQalqLDKoRBGf1NFceGqOpT9o+t/u6fgdNZ
  csuTt/T/ABMGTVb/AFu4NroW37H5rG5upEOB83AUHr9O/r2roI9DtfDsUt1GHmaTBmmc5kLdBz6d
  sdB246XrcIlpKgXaPNOOBz8wqtqkFxreoRWSKRp0ZJuJM43H+6PXPTPufw6DIytCk/0ttauQv2d8
  rbr3Ck8yKPQ9AP7oB7muk1m+S00We5VgVKfKQeDVe80yO3+eFAIO6gf6v6f7P8vpXP63lfsmnbnM
  c8ykoozhepIrlxk3Gi1Hd6L1ehrQinUV9lr92p0PhezNnoMAcfvJcyv9W5orWhKGFDEQU2jaR0xR
  XRCKhFRjsjOUnJuT6mALmOIyxujHMjE4478d6kXUUXIHnAZ/vH8e9FFUIY2roq8+dn03HH86wYby
  3bxFLduji3twYolxk5IyT196KKTipWv0GpNXsbNv4gtYZ/kEgjY/MpXoe5HP/wCv+ZRRTEf/2Q==
_name: !!python/unicode 'Ana Ferreira'
_parent: null
_parent_key: null
_adress: !!python/unicode 'Porto'
_phone: !!python/unicode '1234569789'
_user: *id002
None
Status: 200 OK
Cache-Control: no-cache
Content-Type: text/yaml
Content-Disposition: filename = myYaml.yaml
Expires: Fri, 01 Jan 1990 00:00:00 GMT
Content-Length: 0

Changed the code, but the data is only presented by browser.

Was it helpful?

Solution

Do not use print, use self.response.out.write(...).

Yes, you will want to import yaml to output yaml, it will make it easier.

Try this:

import yaml

users = model.Users.all().fetch(10)
users = [{'user': {'name': user.name,
                   'address': user.address,
                   'phone': user.phone,
                   'city': user.city}}
          for user in users]

self.response.out.write(yaml.dump(users, default_flow_style=False))

You can check out the yaml docs for additional information on formatting the output.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top