{"_id":"59ceb9ff86221200280ec287","project":"5746675a5aa03c0e00fce9bc","version":{"_id":"5746675a5aa03c0e00fce9bf","__v":8,"project":"5746675a5aa03c0e00fce9bc","createdAt":"2016-05-26T03:02:50.849Z","releaseDate":"2016-05-26T03:02:50.849Z","categories":["5746675a5aa03c0e00fce9c0","574899e7e8c6a420000b7314","57489c11d929e919006ed4ae","57489c82d929e919006ed4b1","57489ce91b345d0e004e5231","5748a82e1b345d0e004e5256","59ce5c88e8c105001a4bedd7","59f96cd129b4d600246c2899"],"is_deprecated":false,"is_hidden":false,"is_beta":false,"is_stable":true,"codename":"","version_clean":"1.0.0","version":"1.0"},"category":{"_id":"59ce5c88e8c105001a4bedd7","project":"5746675a5aa03c0e00fce9bc","version":"5746675a5aa03c0e00fce9bf","__v":0,"sync":{"url":"","isSync":false},"reference":false,"createdAt":"2017-09-29T14:45:28.177Z","from_sync":false,"order":1,"slug":"blockspring-scripts","title":"Blockspring Scripts"},"user":"545988354f86a50800f9cce4","__v":0,"parentDoc":null,"updates":[],"next":{"pages":[],"description":""},"createdAt":"2017-09-29T21:24:15.791Z","link_external":false,"link_url":"","githubsync":"","sync_unique":"","hidden":false,"api":{"results":{"codes":[]},"settings":"","auth":"required","params":[],"url":""},"isReference":false,"order":8,"body":"Let's learn about how to create custom reports, and use Python modules.\n[block:api-header]\n{\n  \"title\": \"Usecase: Email Trello Card Assignment Report\"\n}\n[/block]\nMost apps don't provide exactly the right report you might need for yourself, or to share with your team. In this tutorial, we'll write a script that creates and emails a Trello card assignment report at a button click.\n\n## Add a module\nSince our scripts our written in Python, we have access to the entire universe of 3rd party modules through pip. Let's add the `pystache` so that we can easily setup templates for our reporting email.\n\n1) Click **Add+** next to **Modules** in the right sidebar.\n2) Search for your module, in this case **pystache**.\n3) Click the module name, and wait for your script to finish importing.\n4) Type `import pystache` at the top of your script and **Save**.\n[block:image]\n{\n  \"images\": [\n    {\n      \"image\": [\n        \"https://files.readme.io/ddb4d14-importmodule.gif\",\n        \"importmodule.gif\",\n        797,\n        595,\n        \"#0e78b4\"\n      ],\n      \"border\": true\n    }\n  ]\n}\n[/block]\nHaving access to all modules over pip opens the door to many usecases for your script, including fun things with templating, machine learning, visualizations, scraping, and natural language processing.\n\n## ENABLE GMAIL\nNow we'll want to add the Gmail connector to our script. Usually, integrating with another app requires setting up OAuth or getting an API token. Blockspring Scripts handles this so you can start scripting with Gmail in seconds.\n\n1) Click **Add+** next to **Connectors** in the right sidebar.\n2) Choose your connector, in this case **Gmail** .\n[block:image]\n{\n  \"images\": [\n    {\n      \"image\": [\n        \"https://files.readme.io/510c4c7-gmail.gif\",\n        \"gmail.gif\",\n        797,\n        597,\n        \"#0f78b3\"\n      ]\n    }\n  ]\n}\n[/block]\n\n[block:callout]\n{\n  \"type\": \"info\",\n  \"title\": \"Alert - \\\"This app isn't verified\\\"\",\n  \"body\": \"While in beta, Blockspring Scripts will be undergoing Google's verification process. You may notice this message from time to time as we add more connectors.\"\n}\n[/block]\nYou'll notice the editor automatically handles OAuth/authentication with Google, and imports `gmail_app`. You're now able to script with Sheets, just like you do with Trello.\n\n## Email Report\nSince we have the templating module and Gmail integrated in our script, we can now start emailing a report. Paste in the following code into your script.\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"import gmail_app\\nimport trello_app\\nimport pystache\\n\\n# SETUP 1: Sign in with Gmail.\\n# SETUP 2: Enter an email address below.\\nemail = \\\"ENTER_EMAIL_TO_SEND_REPORT_TO\\\"\\n\\ndef send_report():\\n    # Get our Trello board, members, and cards.\\n    board = trello_app.get_current_board()\\n    members = board.get_members()\\n    cards = board.get_cards()\\n\\n    # For each member, find the number of cards they're assigned to.\\n    member_counts = []\\n    for member in members:\\n        card_count = 0\\n        for card in cards:\\n            if member.get_field(\\\"id\\\") in card.get_field(\\\"idMembers\\\"):\\n                card_count += 1\\n        member_counts.append({ \\\"person_name\\\": member.get_field(\\\"fullName\\\"), \\\"card_count\\\": card_count })\\n\\n    # Setup our email template\\n    template = \\\"\\\"\\\"\\nIt's time for your Trello report! According to our calculations, {{board_name}} has {{card_count}} cards. Here's how they're assigned:\\n\\n{{ #counts }}\\n- {{person_name}}: {{card_count}}\\n{{/counts}}\\n\\\"\\\"\\\"\\n\\n    # Populate the template with our member_counts data\\n    message = pystache.render(template, {\\n        \\\"board_name\\\": board.get_name(),\\n        \\\"card_count\\\": len(cards),\\n        \\\"counts\\\": member_counts\\n    })\\n\\n    # Send repot using Gmail\\n    gmail_app.send_message(message, email, subject=\\\"Trello Update\\\")\\n\",\n      \"language\": \"python\"\n    }\n  ]\n}\n[/block]\nSince we've already signed in with Gmail, the next step is to enter your email address in line 7.\n\nFinally, let's run our `send_report`. However, instead of using the test button in the editor, let's go back to Trello.\n\n1) Hover over the Blockspring leaf on your Trello board,\n2) Click the send_report menu option.\n\nCheck out your email - it has our report!\n[block:image]\n{\n  \"images\": [\n    {\n      \"image\": [\n        \"https://files.readme.io/70ded19-report.gif\",\n        \"report.gif\",\n        920,\n        761,\n        \"#4fb76d\"\n      ],\n      \"border\": true\n    }\n  ]\n}\n[/block]\n\n[block:api-header]\n{\n  \"title\": \"Need Help?\"\n}\n[/block]\nIf you run into any issues, or what to chat with others, [connect to the Blockspring community](https://blockspring-help.readme.io/docs/getting-help) right from the Scripts editor.","excerpt":"","slug":"tutorial-app-modules","type":"basic","title":"Tutorial: Custom Reports and Modules"}

Tutorial: Custom Reports and Modules


Let's learn about how to create custom reports, and use Python modules. [block:api-header] { "title": "Usecase: Email Trello Card Assignment Report" } [/block] Most apps don't provide exactly the right report you might need for yourself, or to share with your team. In this tutorial, we'll write a script that creates and emails a Trello card assignment report at a button click. ## Add a module Since our scripts our written in Python, we have access to the entire universe of 3rd party modules through pip. Let's add the `pystache` so that we can easily setup templates for our reporting email. 1) Click **Add+** next to **Modules** in the right sidebar. 2) Search for your module, in this case **pystache**. 3) Click the module name, and wait for your script to finish importing. 4) Type `import pystache` at the top of your script and **Save**. [block:image] { "images": [ { "image": [ "https://files.readme.io/ddb4d14-importmodule.gif", "importmodule.gif", 797, 595, "#0e78b4" ], "border": true } ] } [/block] Having access to all modules over pip opens the door to many usecases for your script, including fun things with templating, machine learning, visualizations, scraping, and natural language processing. ## ENABLE GMAIL Now we'll want to add the Gmail connector to our script. Usually, integrating with another app requires setting up OAuth or getting an API token. Blockspring Scripts handles this so you can start scripting with Gmail in seconds. 1) Click **Add+** next to **Connectors** in the right sidebar. 2) Choose your connector, in this case **Gmail** . [block:image] { "images": [ { "image": [ "https://files.readme.io/510c4c7-gmail.gif", "gmail.gif", 797, 597, "#0f78b3" ] } ] } [/block] [block:callout] { "type": "info", "title": "Alert - \"This app isn't verified\"", "body": "While in beta, Blockspring Scripts will be undergoing Google's verification process. You may notice this message from time to time as we add more connectors." } [/block] You'll notice the editor automatically handles OAuth/authentication with Google, and imports `gmail_app`. You're now able to script with Sheets, just like you do with Trello. ## Email Report Since we have the templating module and Gmail integrated in our script, we can now start emailing a report. Paste in the following code into your script. [block:code] { "codes": [ { "code": "import gmail_app\nimport trello_app\nimport pystache\n\n# SETUP 1: Sign in with Gmail.\n# SETUP 2: Enter an email address below.\nemail = \"ENTER_EMAIL_TO_SEND_REPORT_TO\"\n\ndef send_report():\n # Get our Trello board, members, and cards.\n board = trello_app.get_current_board()\n members = board.get_members()\n cards = board.get_cards()\n\n # For each member, find the number of cards they're assigned to.\n member_counts = []\n for member in members:\n card_count = 0\n for card in cards:\n if member.get_field(\"id\") in card.get_field(\"idMembers\"):\n card_count += 1\n member_counts.append({ \"person_name\": member.get_field(\"fullName\"), \"card_count\": card_count })\n\n # Setup our email template\n template = \"\"\"\nIt's time for your Trello report! According to our calculations, {{board_name}} has {{card_count}} cards. Here's how they're assigned:\n\n{{ #counts }}\n- {{person_name}}: {{card_count}}\n{{/counts}}\n\"\"\"\n\n # Populate the template with our member_counts data\n message = pystache.render(template, {\n \"board_name\": board.get_name(),\n \"card_count\": len(cards),\n \"counts\": member_counts\n })\n\n # Send repot using Gmail\n gmail_app.send_message(message, email, subject=\"Trello Update\")\n", "language": "python" } ] } [/block] Since we've already signed in with Gmail, the next step is to enter your email address in line 7. Finally, let's run our `send_report`. However, instead of using the test button in the editor, let's go back to Trello. 1) Hover over the Blockspring leaf on your Trello board, 2) Click the send_report menu option. Check out your email - it has our report! [block:image] { "images": [ { "image": [ "https://files.readme.io/70ded19-report.gif", "report.gif", 920, 761, "#4fb76d" ], "border": true } ] } [/block] [block:api-header] { "title": "Need Help?" } [/block] If you run into any issues, or what to chat with others, [connect to the Blockspring community](https://blockspring-help.readme.io/docs/getting-help) right from the Scripts editor.