Can bubble run python scripts?

Is it possible for bubble to run python scripts?

8 Likes

Hi @orensaldanha

You canā€™t directly do this that I am aware of, but you do have other options close to hand.
Both Zapier and Blockspring allow you to write simple Python scripts and it is pretty easy to trigger those from Bubble. Help pages pasted below.

If your Python script is a little more complex then you might be looking to host on a dedicated server and make a call to it from Bubble. Something like PythonAnywhere allows you to simply to host your code and you would make an API call to it from Bubble, once you have setup an endpoint in your Python script.


https://www.blockspring.com/docs/python-quickstart
https://www.pythonanywhere.com/

Ed

13 Likes

Woah, I think thatā€™s exactly what I want !

Cant wait to try it!

Thanks a lot, Edward

Thanks a lot for this idea. Pythonanywhere helped me immensely.

1 Like

Follow up - my Pythonanywhere Flask app console seems to need a restart every week or so, as it appears to stop running. If you have experience, do Pythonanywhere apps have bad uptime, or am I just using it incorrectly?

Hey @spencer,

Canā€™t comment on pythonanywhere, but Iā€™ve really been happy with Google Cloud functions now that they added Python 3.7 runtimes. If you canā€™t find a pythonanywhere solution, you might check into this, or Lambda if Amazon is your infrastructure choice.

1 Like

Have you tried using their scheduled tasks or their ā€˜Always Onā€™ tasks?

1 Like

Thanks - I think thatā€™s going to be a winner.

1 Like

Iā€™m curious to know what some of your use-cases are for running python scripts? Has anyone made a machine learning app?

Sure, hereā€™s one use case ā€“ CSV uploads:

CSV uploads have too many limitations right now in Bubble, so I offload that to python. A user uploads a CSV file, which I send off to python. Python parses the headers and evaluates the data, and then returns to Bubble the column headers. I take those column headers and allow the user to map those fields, to Bubble fields. This is done so the user does not have to provide a CSV with exact matches in the header row.

Once the mapping is done, I give python the map and have it perform the actual upload into the Bubble db. It breaks the CSV into batches that conform to any limits on the Bubble side, and uses the data api to send it all over ā€“ or alternatively, it does individual posts of new data in the case of needing to check for dupes, modify existing records, etc.

When finished, I trigger a post back to a Bubble workflow that generates an alert to the user so they know the process is complete.

Along those same lines, there is a nightly polling of a remote ftp server, that spins through any files and processes them much like I described above.

Another use case is dealing with unique identifiers ā€“ I basically use python and a mysql db to do record locking in situations where race conditions occur, or when I need very tight assurance that a value is not used twice.

6 Likes

I use python for a number of things. For instance, I have a discord bot written in python which interacts with bubble as the frontend experience and allows me to create a more holistic experience for users.

Similarly, I use python scripts for dynamic bulk data creation to bubble since the 'Schedule a workflow on a list" and ā€œrecursive schedulingā€ can be slow at times.

Essentially, I use python to address some of the bubbleā€™s limitations without having to worry about maintaining a whole codebase that comes with building the whole application with code.

Re: Machine Learning. Thatā€™s something I intend to try my hand at in the coming weeks with the bot.

4 Likes

I just use it to interact with an API using a set of logic that Bubble canā€™t provide. I POST to the same endpoint in a loop until it returns a success code, and then I GET from a different endpoint with data from the first result.

2 Likes

Hey guys wonder if anyone can help me on this -

I want to grab some numbers saved in my Bubble database, use some python code (or any other way you can suggest) to perform some if/or logic and calculations on the numbers. The output would then be a number that I want to save back into my database.

Bit stuck on this and would appreciate any help on it! Cheers

Unless youā€™re doing complex computation on large datasets then the easiest way to do this is locally using Javascript.

The simplest way to do that in Bubble is to use the Toolbox plugin and use the Run javascript workflow action.

You can bring variables or arrays into your script just how you would normally bring dynamic data into text / search fields.

3 Likes

Thanks @exception-rambler that looks like exactly what I need, certainly no complex computation.

Guess Iā€™ll have to learn some Javascript!

Really appreciate the help, many thanks :facepunch:

Happy to help if you get caught on the script - DM me or put it in a new thread here.
But having some Javascript skills will grant you Bubble superpowers so itā€™s time well spent.

2 Likes

Thanks again @exception-rambler - one thing Iā€™m not sure of is how to handle cases where there is no value in the database?

Say for example I want to grab a users age and run some logic on it, but if they havenā€™t entered their age then use a default value of 0

When Iā€™m building plugins Iā€™ll normally include a function that helps me cover the different possibilities of no value. As follows:

function isEmpty(property){
  if (property === '' || property === null || property === undefined) return true;
  else return false;
}

You could add this at the start of your script and then say something like:

if (isEmpty(**Search for your item:first item**)) let newValue = 0;
else { **calculate new value**}

One thing that isnā€™t 100% straightforward is getting a value back out of your script to use later on.

To do this youā€™ll need to an a Javascript to Bubble element to your page (this will have come with Toolbox). This workaround is needed because Bubble carefully controls how you can get data back into your workflows / database, so it has to come via a plugin.

Set it up as follows - Iā€™ve used a generic name ā€˜fetchValueā€™, you could use anything:

Then finally, back in your workflow, you will finish the script with:

bubble_fn_fetchValue(newValue); where newValue is the name of the variable carrying your adjusted number.

That value will now be available throughout Bubble - either in a Text element / something similar, or in a workflow - by referencing JavascripttoBubbleAā€™s value

4 Likes

Once again @exception-rambler, thanks a million for this. All working great for me now, really appreciate the detailed explanation and think that your isEmpty function will quickly become a staple of mineā€¦

Hey @exception-rambler - another question I hoped youā€™d be able to help out with. Wanted to know how private the code I put in my Javascript script will be?

Iā€™m putting some business logic in there which ideally wouldnā€™t be public so just wanted to checkā€¦

Cheers, Lee