+2  A: 

The best way to talk from Flex to GAE is using AMF. Here is how:

app.yaml

application: flexandthecloud
version: 3
runtime: python
api_version: 1

handlers:
- url: /services/.*
  script: main.py

main.py

#!/usr/bin/env python
import wsgiref.handlers

from pyamf.remoting.gateway.wsgi import WSGIGateway

def sayHello(name):
  return "howdy " + name

services = {
    'services.sayHello': sayHello
}

def main():
  application = WSGIGateway(services)
  wsgiref.handlers.CGIHandler().run(application)

if __name__ == '__main__':
  main()

Flex 3 code (can be easily modified for Flex 4):

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"&gt;

    <mx:RemoteObject id="ro" destination="services" endpoint="http://flexandthecloud.appspot.com/services/"&gt;
        <mx:result>
            l.text = event.result as String;
        </mx:result>
    </mx:RemoteObject>

    <mx:TextInput id="ti"/>
    <mx:Label id="l"/>
    <mx:Button label="say hello" click="ro.sayHello(ti.text)"/>

</mx:Application>
James Ward
Thanks James. Actually, I am using pyamf for the gateway. I am trying to pass some data to the flex application when the html renedered, so that I do not need to call any service and I can get some data.
michael
I would just use an applicationComplete event on Application that would call the service and load the data.
James Ward
Yes, you can, but you need to connect the app engine once more. This will increase the overhead.
michael
+1  A: 

What's in your index.html? You can pass the values to index.html, set a javascript function like:

function passvalue {
    PassParameter("userVO", "{{userVO}}");
}

Then set a function in Flex:

public function gethtmlparam(name:String, val:String):void {
            switch (name) {
                case "userVO": userVO= val; break;}
}

and call back these two function in :

ExternalInterface.addCallback("PassParameter", gethtmlparam);

Hope it can help.

mal39
mal39, thanks for your reply. It's a good try. However, I am testing how the python app server pass data to the flex app through the swfobject. Maybe, my question is not clear enough.For the index.html, it is a standard file generated by Flash Builder automatically.Anyway, thanks for your suggestion.
michael
+1  A: 

Based on your comment to James Ward's response, I wonder if you can accomplish what you need with a FlashVars param element?

http://livedocs.adobe.com/flex/3/html/help.html?content=passingarguments_3.html

You will probably just need to adjust the index.html template to build the FlashVars param element.

Edit: 6 Mar

You might try looking at: http://polygeek.com/801_flex_reading-flashvars-in-flex

You need to be sure you wait until the app has been loaded to access the flashVars.

Edit: 7 Mar

Correct, in those examples they hard code the value. You need to edit your template so the value is set to the value of the template parameter.

So, in index.html: flashvars.userVO = "{{ userVO }}"

Robert Kluin
Thanks Robert, I know I may adjust the Flashvars to accomplish the task and I have tried it many times. But, it do not success. For the livedocs, both flex 3 and 4, I have already read it several times.
michael
Can you post the contents of your index.html template as well?
Robert Kluin
Robert, I post the file in the question.
michael
The link is not suitable for my case, because it create the flashvar by hardcoded the flashvar in the file. For my case, I need to pass the variable from the server to the flex app dynamically.
michael
I updated the answer with more detail, but you may also want to see:http://code.google.com/appengine/docs/python/gettingstarted/templates.html
Robert Kluin
I tried: flashvars.userVO = "{{ userVO }}", but nothing return.
michael
When you 'view source' in your browser is the correct value there?
Robert Kluin
no value in there.
michael