Skip to main content

Python and click answers

Folders examples/Conditional and examples/Scripts. The Python examples need a Python installation on the server: see Python in the overview.

Create Pin​

Beginner examples/Scripts/Create Pin

The smallest diagram with a script: a Python node creates a random four-digit PIN with the secrets module, padded with zeros, and a CSV node stores it. Open it first if you want to see how the result of a script becomes a variable.

Needs: Python.

Prompt

Create a diagram with a Python script that creates a random four-digit PIN with the secrets module (zero-padded) and returns it as the result. Write the result to the file pin.csv and end.

Python Loop​

Beginner examples/Conditional/Python Loop

A script that counts. Each pass reads the counter from the variable script_result (0 the first time), adds one, writes it to the log and sets action = 'exit' when it exceeds 10. The decision behind the script ends the diagram on exit, otherwise the counter is stored in counter.csv and the script runs again.

Needs: Python.

Prompt

Create a diagram with a Python script node that counts: read the variable script_result (0 if it does not exist), add 1 and return it as the result, print it to the log and set the variable action to "exit" when the counter is greater than 10. Behind the script a decision: "exit" ends the diagram, "next" writes the counter to a CSV file and goes back to the script.

Python Router​

Intermediate examples/Conditional/Python Router

The script chooses the edge. It computes the Collatz sequence of 27 in batches of 25 steps and continues where the last round stopped, using the variables n, steps, peak and rounds that it writes as a dictionary. After each batch it sets action to again (next batch), converged (the sequence reached 1) or give up (after eight rounds). The decision behind the script has one edge of each name and no condition: a decision without a class follows the way the script chose. Read Python, section Action.

Needs: Python.

Prompt

Create a diagram with a Python script that computes the Collatz sequence of 27 in batches of 25 steps. Each round it continues from the variables n, steps, peak and rounds of the previous round (start values 27, 0, 27, 0) and returns the new values as a dictionary. Set the variable action to "converged" when n is 1, to "give up" after 8 rounds, otherwise to "again". Behind the script a decision with the edges "converged" and "give up", each to its own end node, and "again" back to the script. The edges need no conditions.

Click Answer​

Intermediate examples/Conditional/Click Answer

A survey mailing. For every person of a list a parallel path sends a mail with three links: survey, no participation and opt out. The path sleeps until a link is clicked. survey sends the survey mail, opt out appends the person to a list of opt-outs, no participation and a timeout end the path. The main path reads on with the next person. See Click response and Fork.

Needs: mail sending and the hostname; the example data examples/Data/address/Fictional addresses.csv. The recipients are constructed (Surname@email): run it as a dry run (it sends all mails to the sender then). The opt-outs are written to opt_out.csv in the output folder.

Prompt

Create a survey mailing. Read the persons of examples/Data/address/Fictional addresses.csv row by row. For each person fork a parallel path that sends an e-mail with three answer links ${action.survey}, ${action.no participation} and ${action.opt out} and a click response decision behind it. "survey" sends the survey mail and ends the path, "opt out" appends the person to the file opt_out.csv and ends the path, "no participation" and "timeout" end the path. The main path goes on with the next row; when no row is left the diagram ends.