Is there a way to call Python function from js?

I try to use TAL but that too didn't allow much interaction with JS. I tried this hack, but it also didn't suffice my requirement.
I need a pure JS Python interaction in Plone.

The proposed solutions are the way to go and working since the earliest days...why is this not good enough for you?

-aj

which proposed solutions are you talking to about? Can you give a link to it?

You cannot directly invoke a Python function from JS: JS is executed in your browser; Python is executed in your server. In order to call a Python function (in your server) from JS (in your browser), you need an interaction from the browser to the server. Many JS frameworks (e.g. jquery) provide a means for such an interaction. The Python function is likely exposed as a view, such that it can be called via the web.

Right this sounds good. Any working example of this? With clear explanation of usage is what I really need.

I don't have enough context to give a useful answer.

To help with some context, can you share the code that you're working on? Specifically what does it do and where are you trying to run this?
Also specifically where does it not work....

Here is the implementation in tal, which is static.

While now the need is to dynamically update the client-side table data, every time a new file has been uploaded.

Surely JS could help me here and not TAL. Thus require a way to call python function view.getImportfields(uploadedFile).values() from Js at here

What you're looking for is some kind of xmlhttprequest to getImportfields

I'd create a new browserview that returns getImportfields (the example below could be added to your configure.zcml to create a view called "import-fields":

<browser:page
        for="Products.CMFCore.interfaces.IFolderish"
        name="import-fields"
        class=".importexport.getImportfields"
        permission="cmf.ModifyPortalContent"
        />

You don't need a template.

This view would be accessible via @@import-fields

This could then be called via an xmlhttprequest in your tal template.

You cited the Hack link yourself.

—aj

I cannot use tal, it provides a static solution. Does the above solution support js call?

What does it mean?

I'm making a few assumptions, which may be unfair.

The major assumption I'm making is that you already know how to make an XMLHTTPRequest type JS call to a web based endpoint. If that's something you aren't familiar with, that's fine. I'd say that is what you need to get comfortable with.

My notes above explain how to setup a browserview by reusing an existing function that you are already using (getImportfields). Once you're fine with XMLHTTPRequests you should be able to do what you need with the proposed browserview.

1 Like

OK, thanks. :slight_smile: Will get back to you in case of any error

Make an Ajax request and encode your data as JSON....this is widely used.

-aj

Can it be done like this (where "render_from_markdown" is a browserview?

2 Likes

IMO This is a good example, it uses jqueries builtin ajax/xmlhttprequest capabilities $.post() to call a browserview called @@render_from_markdown.

In my suggested example render_from_markdown would be replaced with some browserview associated with 'getImportfields'. Then just make the code do what you need it to do.

1 Like

Thank people it worked. The issue is been closed now. :slight_smile: