Frappe Builder Data Script examples

Hi

I’m following the Data Script documentation at Data Script

However, many of the code snippets does not work, such as

data.users = frappe.get_all("User")

which should actually use

frappe.db.get_all()

But I cannot understand / resolve what is wrong with

data.posts = frappe.make_get_request("https://jsonplaceholder.typicode.com/posts")

If I copy https://jsonplaceholder.typicode.com/posts into my browser’s URL bar, it does return the expected JSON. So the REST API is correct. Clearly there’s something wrong with frappe.make_get_request()

Can someone please help.

Hi @EugeneP:

It’s working for me …
Have you enabled server_script for your site?

bench set-config server_script_enabled 1

Hope this helps.

Hi @avc

Good to hear from you again

Yip, it’s been activated, otherwise I would not have been able to execute other methods such as getting the currency exchange rate and gold price from Yahoo Finance

from yahooquery import Ticker as yq
data.yf = yq("ZAR=X GC=F").summary_detail

Can you inspect the response on browser dev tools? Can you see fetched data there?

I cannot Save the Data Script.
In order to Save the script, I have to comment out the line where I call frappe.make_get_request.
So I cannot even test it.

ksnip_20250209-205034

How can I test it in DevTools when I click Save on the Data Script modal window?

AFAIK, import is not allowed on this kind of scripts …
Which error get you on saving?

A red notification at the bottom right saying that there was an error while fetching data.

The YF data is returned correctly, so the import must be working.

ksnip_20250209-211224

Even if I remove all other code and only have the frappe.make_get_request I still cannot save the script.

Mr Bean Shocked GIFs - Find & Share on GIPHY

Theorically, only this should be allowed …
https://docs.frappe.io/framework/user/en/desk/scripting/script-api

There’s something odd going on with your installation. I just checked Frappe Build on my site (v15 latest) and got different results from you.

Both

data.users = frappe.get_all("User")

and

data.posts = frappe.make_get_request("https://jsonplaceholder.typicode.com/posts")

work fine for me.

Like @avc and Mr. Bean, I’m also very surprised that import statements are working for you. I didn’t think that was possible last time I checked.

Hi @peterg

Yip, something very strange…

ksnip_20250210-060016

Why would frappe.get_all not work but frappe.db.get_all does?

And frappe.make_get_request not at all?

If I’m not mistaken, the builder develop branch targets Frappe v16, not v15. I think that’s probably your issue. I haven’t been following it closely, but I know there are some big API changes coming in v16.

All the releases are from the master branch. If you switch to that, I suspect it will start working for you.

I’d be curious to know if this was the issue. Let us know, @EugeneP!

I did not plan / contemplate changing the branch, but with you asking for such indirectly, here’s the outcome:

bench switch-to-branch master --upgrade builder
bench update --patch
bench restart

ksnip_20250213-055357

To ensure and confirm the Data Script executes afresh, I added the stock symbol for Google (GOOG) to the Yahoo FInance query.

data.meep = [{'c':'m'},{'c':'e'},{'c':'e'},{'c':'p'}]
data.users_gl = frappe.db.get_list("User")
data.users_ga = frappe.db.get_all("User", fields=['*'])
from yahooquery import Ticker as yq
data.yf = yq("ZAR=X GC=F GOOG").summary_detail
# data.rest = frappe.make_get_request("https://jsonplaceholder.typicode.com/posts")

As can be seen below, it did.

ksnip_20250213-055132

However, the moment I uncomment the final

frappe.make_get_request

I get an error when clicking the Save button at the bottom of the Data Script editor, stating that “There was an error while fetching page data”.

Also note that I cannot change the

frappe.db.get_list

to

frappe.get_list

without the same error message when clicking Save.

All I can say is : It’s bizarre.

Yeah, at this point I think it’s fair to say you’ve got a non-working installation. If you can reproduce the issue, it’d be worth posting to the repo.

One other thought: you’ve switched the branch, but it’s not clear here if the builder site has been recompiled. It might be necessary to run bench build as well.

I ran

bench build

and it all succeeded. I then did

bench reload

and there is no change to the behaviour. The YF query still runs as previously and the 2 frappe invocations still cause an error.

Hmm…I just installed the builder develop branch on my development instance to test.


Results as expected:

It definitely seems like your installation is borked in some way or another. If this is a builder bug, it’s not one I can reproduce.