cr-connect-tutorial/03_cr_connect_workflow/01_scripts.rst

99 lines
3.3 KiB
ReStructuredText
Raw Normal View History

2021-05-10 17:38:15 +00:00
=================
Creating a Script
=================
2021-05-10 20:10:57 +00:00
`Scripts` can be called from a script task in workflows. They are a great way to extend workflow capabilities.
Scripts extend the `Script` base class found in crc.scripts.script, and they must define the
`get_description`, `do_task`, and `do_task_validate_only` methods.
The get_description method should return a string that is displayed when configurators get a list of available scripts.
The do_task_validate_only method is run during the configurator `shield test`.
We only need to make sure that the script *can* run here. We don't want to make any externals calls.
The do_task method is where we put the code we need to achieve our task.
--------------
Example Script
--------------
Create a script that accepts keyword arguments, calls an external api using the arguments, and returns a result.
My data source is a simple API found at https://deckofcardsapi.com that returns cards drawn from one or more decks of playing cards.
My script accepts two keyword arguments; cards and decks.
2021-05-10 17:38:15 +00:00
Example code: crc/scripts/tutorial.py
.. code-block:: Python
from crc.scripts.script import Script
import requests
class TutorialScript(Script):
def get_description(self):
return """Simple script for teaching purposes"""
def do_task_validate_only(self, task, study_id, workflow_id, *args, **kwargs):
self.do_task(task, study_id, workflow_id, *args, **kwargs)
def do_task(self, task, study_id, workflow_id, *args, **kwargs):
2021-05-10 20:10:57 +00:00
2021-05-10 17:38:15 +00:00
drawn_cards = []
2021-05-10 20:10:57 +00:00
cards = int(kwargs['cards']) if hasattr(kwargs, 'cards') else 1
decks = int(kwargs['decks']) if hasattr(kwargs, 'decks') else 1
2021-05-10 17:38:15 +00:00
deck_url = f'https://deckofcardsapi.com/api/deck/new/shuffle/?deck_count={decks}'
deck_response = requests.get(deck_url)
deck_id = deck_response.json()['deck_id']
card_url = f'https://deckofcardsapi.com/api/deck/{deck_id}/draw/?count={cards}'
card_response = requests.get(card_url)
for card in range(cards):
card_value = card_response.json()['cards'][card]['value']
card_suit = card_response.json()['cards'][card]['suit']
drawn_cards.append({'suit': card_suit, 'value': card_value})
return drawn_cards
2021-05-10 20:10:57 +00:00
First, I create an empty list of drawn_cards, and pull cards and decks from the keyword arguments.
Cards and decks both default to 1 if they don't exist as keyword arguments.
I make an API call to get a `deck_id`, and then make another call with the deck_id to get a `card_response`.
From the card_response, I build and return a list of drawn_cards.
We have a script we can call from a workflow. Now we have to build the workflow.
----------------
Example Workflow
----------------
Here is a simple workflow you can use to call this script.
2021-05-25 14:33:45 +00:00
.. image:: /_static/03/01/tutorial_workflow.png
2021-05-10 20:10:57 +00:00
2021-05-25 14:33:45 +00:00
.. image:: /_static/03/01/how_many.png
2021-05-10 20:10:57 +00:00
2021-05-25 14:33:45 +00:00
.. image:: /_static/03/01/get_cards.png
2021-05-10 20:10:57 +00:00
2021-05-25 14:33:45 +00:00
.. image:: /_static/03/01/display_cards.png
2021-05-10 20:10:57 +00:00
And here it is running.
2021-05-25 14:33:45 +00:00
.. image:: /_static/03/01/start_workflow.png
2021-05-10 20:10:57 +00:00
2021-05-25 14:33:45 +00:00
.. image:: /_static/03/01/enter_how_many.png
2021-05-10 20:10:57 +00:00
2021-05-25 14:33:45 +00:00
.. image:: /_static/03/01/display_returned_cards.png
2021-05-10 20:10:57 +00:00
2021-05-10 20:37:39 +00:00
You can find the :download:`bpmn for the workflow <https://github.com/sartography/cr-connect-tutorial/blob/main/_static/tutorial.bpmn>` in the repository.
2021-05-10 17:38:15 +00:00