0

In short: I'm looking is to get the img src value onClick() & pass that selected image to a python function to work with PIL

say, var i = getElementById("image-id").src pass this to python def whatever(): img = src var from js fun

Is it possible? I know this can be done >>>

def post(self):
    val = self.request.get("name_of_the_txt_field_to_get")
    self.request.write("val") 

but I want the variable to a be an image SRC with a onClick(); button function, and I want to pass this SRC value to the python function as a variable.

I'm aware that js is client side & python is server side, So I'm looking to pass the value to another URL say from http://localserver to http://localserver/image

I've been finding a solution to it & I did came across some options:https://github.com/atsepkov/RapydScript (but it's a python to javascript compiler & I'll need to compile it)

another one is the good old http://code.google.com/p/pyv8/ but I can't figure it out &

this one seems to be pretty interesting PICO & I think would get the job done, however I can't figure it on how do I run it on GAE

workspace: GAE for python with Jinja2 Template.

0

2 Answers 2

0

You could use jQuery to do an AJAX POST to http://localserver/image:

$(document).ready(function() {
    $("img").click(function() {
        var request = $.ajax({
            url: "/image",
            type: "POST",
            data: {imageSrc : $(this).attr("src")}
        });

        request.done(function(msg) {
            alert("Request succeeded: " + msg );
        });

        request.fail(function(jqXHR, textStatus) {
            alert( "Request failed: " + textStatus );
        });
    });
});

See also: Ajax tutorial for post and get

4
  • yeah I thought about ajax, but was just thinking can this be achieved by python? Sep 14, 2013 at 16:23
  • 1
    You are asking a question that doesnt seem to make sense. What do you mean by doing it with python? That is on the server, how could it possibly access the dom on the users browser. Only js runs on a normal user browser.
    – Zig Mandel
    Sep 14, 2013 at 17:07
  • @ZigMandel yeah sort of, I just wanted to get my idea across, onClick in python to get img src Sep 14, 2013 at 17:23
  • Sorry question still doesnt make sense.
    – Zig Mandel
    Sep 15, 2013 at 15:31
0

I'm assuming you're asking about Javascript because you're running something in a web browser. And when you run something in a browser, I assume you want things to run in any browser.

In which case, the answer is no, there isn't.

Browsers can execute Javascript. They don't execute python. So you will need to execute Javascript in the browser, make some sort of HTTP request to your server, and process the request in python. There's libraries like pico that generate wrappers so you have to write less code, but it does the same thing in the background.

You could write some sort of plugin for browsers that can execute python. It's been done: https://wiki.python.org/moin/WebBrowserProgramming

But you'll be limited to a subset of browsers who have the appropriate plugin installed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.