#how to use flow scripts?

1 messages · Page 1 of 1 (latest)

zenith comet
#

Hi, I'm new to webflow and my first use case is to create a bill of materials for a multi-component product, so i'm using a manual flow to compute the price of all sub-parts.
Can you please point to any documentation on how to use scripts within flows ? I see that I can run some javascript in there but i'm not sure what I can access and how to test it easily.
Also interested in how to do what I need without scripts, but I will eventually need scripts anyway.

Thank you!

lilac flare
#

this is an example of an invoice flow I use for my book keeping that uses run script to calculate a subtotal from each item in a repeater if it helps. There are new docs related to flows being worked on currently

#

cc @hard sky

hard sky
# zenith comet Hi, I'm new to webflow and my first use case is to create a bill of materials fo...

Hey there!

i'm not sure what I can access

If you're on Directus Cloud, you cannot access libraries quite yet.
If you're self-hosted, I believe we still advise against using libs/imports

I'm actually not sure if any Directus services can be accessed within a Run Script 🤔
Do they @lilac flare ? I'm not 100% how all that works yet.

(p.s. flows run in a node env, not a window env)

i'm not sure....how to test it easily.

For easy & simple testing, you can use the logs.
https://docs.directus.io/configuration/flows.html#logs
Whatever you return in a Run Script is appended onto the data chain under its respective variable. You can run the flow and go check it in the logs.

Let me know if that works out, or if you have any other thoughts/feedback.

lilac flare
#

Usually people just do an api post or get if they need specific directus services, otherwise the example I gave will read the posted object and alter it before saving

zenith comet
#

this is where i'm standing but it feels i'm not doing it in an efficient way. It fetches data from a table, runs a script to get a list of IDs, that i can pass to the next data read to select in the table only relevant items, and a third time the same ; then i would need to create a last script that multiplies price * items_by_subassembly * subassembly_by_assembly

#

my main feedback after a day on Directus, experimenting the flows, would be to be able to run the script directly from the flow view instead of keeping a tab open to trigger it in the relevant collection, and being able to see logs in real time as i need to refresh the page (~4s) to check the logs

lilac flare
#

Depends on what the run script does but looks about typical for some larger flows

zenith comet
#

I'm a software developer and I feel a bit "blind" working in the flows at the moment, partly because i can't log as much as I usually do to see what i'm working on, and partly because the code editor in the right part of the screen obscures the flow so i can't look at it while coding, also the output variable names of flow steps are not shown on the flow view so i need to get out of the code editor, open the flow item to check the variable name

lilac flare
#

yes. flows are still new so it's a bit of a learning curve. when I did the example I posted above, there was lots of trial and error originally.

hard sky
zenith comet
#

otherwise i'm really amazed by directus, it's basically the only alternative i found to my usual python/golang/vuejs dev requirements

lilac flare
#

but that's also why I cc'ed @hard sky as he is working on our flows docs with new stuff coming 🙂

hard sky
zenith comet
#

I've refactored my models as to properly use the M2M fields and get with one request all the data that I need. I've written a python script which does what I needed, is there a proper way to do that with flows, so that I can use the output in a dashboard to track the manufacturing price of a product?

import requests
# import json

url = "http://localhost:8055"

token = "zgEECXXXXXXXXXXXXXTZlPWsJ79182" # localhost admin

headers = { 'Authorization': f'Bearer {token}' }

def req(path):
  ret = requests.get(f"{url}{path}", headers=headers)
  if ret.status_code != 200:
    print(ret.json())
    return ret.json()
  else:
    # print(json.dumps(ret.json()['data'], indent=2))
    return ret.json()['data']

product = 'Nova'
data = req(f'/items/products?filter[description][_eq]={product}&fields=*,assemblies.*,assemblies.assemblies_id.*,assemblies.assemblies_id.parts.*,assemblies.assemblies_id.parts.parts_id.*,assemblies.assemblies_id.parts.parts_id.selected_supplier.*')[0]

total_price = 0

for assembly in data['assemblies']:
  assembly_price = 0
  for part in assembly['assemblies_id']['parts']:
    price = part['parts_id']['selected_supplier']['price']
    assembly_price += float(part['quantity']) * float(price)
  total_price += float(assembly['quantity']) * float(assembly_price)

print(total_price)
zenith comet
#

Also, as a vim user, i've just hurt myself badly by pressing escape on an unsaved flow script 🥲