Quickstart: Add your first Python function to Blockspring.

In this tutorial we'll show you how to add a Python function to Blockspring. Doing so is helpful if you need to:

  • Expose your function to the world. Need some Python in your Rails app, iOS app, Arduino, or even in Google Spreadsheets? You'll be able to run your function from any application and device by simply copy/pasting a single line of code.

  • Share your function with anyone. You'll automatically generate a simple form and webpage documentation. Never spend time explaining your code, or running it for other people.

  • Combine Blockspring functions together. You can mix/match Blockspring functions like building blocks. And the system gets really fast when it knows ahead of time which functions you're about to run together.

  • Add really, really powerful caching. Don't just cache when you run a function, use Blockspring's social caching. What's the point of recalculating when someone's already done the calculating?

Let's get started!


Expose your function to the world.


This is going to be insanely fast. In 30 seconds, you're going to add a function to Blockspring that scrapes text from Yahoo.com.

import urllib
from bs4 import BeautifulSoup

html = urllib.urlopen("https://www.yahoo.com").read()
soup = BeautifulSoup(html)

for script in soup(["script", "style"]):
    script.extract()

text = soup.get_text()

print text.encode("utf-8")

Click to populate the Blockspring editor with the code above, click Publish, and you're done!


What just happened?

When you published your function, Blockspring automatically deployed it to the cloud, gave it an API, generated a webpage, and exposed it to all of the Blockspring client libraries.


Why is this awesome?

Your Python function is now in the cloud. That means you can run it from any application or internet-enabled device with a single command. What once took 6 lines of Python, now is 1 line in any language. For instance, to run your scraper in Node.js, just type the following:

var blockspring = require("blockspring");

// call scraper (https://open.blockspring.com/blocks/c959e1c889c10478f118140fa39bb1d0)
blockspring.runParsed("c959e1c889c10478f118140fa39bb1d0", function(res){ console.log(res); });


Where do I learn more about using my function?

Your published function should like something like this scraper. On each Blockspring function page you can click "Integrations", and depending on where you need to use your function, copy/paste the associated one line of code to run it. Check out the run Python in Blockspring quickstart to learn more.


That was fast. Can you show me how to publish again?

Here's a quick video that shows you how to publish step-by-step.


Share your function with anyone.


Blockspring lets you share your work with programmers and non-programmers alike because we generate the webpage and docs.

Currently, the webpage for your scraper function is just a "Submit" button. That's because this function doesn't take any inputs.

Submit only form

Let's tell Blockspring that your function accepts inputs. For instance, try scraping any website, not just Yahoo.

Click to populate the Blockspring editor with the code below, click Publish, and you'll have a nice form for your function.

import urllib
from bs4 import BeautifulSoup
import blockspring

def block(request, response):
    html = urllib.urlopen(request.params["url"]).read()
    soup = BeautifulSoup(html)

    for script in soup(["script", "style"]):
        script.extract()

    text = soup.get_text()

    response.addOutput("text", text.encode("utf-8"))
    response.end()

blockspring.define(block)

Take a look at the final product. Here's a video that shows the process step-by-step:



What just happened?

You updated your function in four ways:

  • Import the library. You imported the blockspring library with import blockspring. Blockspring captures outputs by reading what is printed. That's why we're suppressing loading messages.
  • Define the function. You wrapped the function in a blockspring.define(). This tells Blockspring what function run.
  • Get the inputs. Receiving the url input in request.params["url"]. All function inputs will be found in request.params. The "url" name is defined when you added a parameter through the Blockspring code editor.
  • Declare the outputs. Adding the scraped output using response.addOutput. Blockspring passes inputs and outputs as JSON. This is adding your output to the JSON response.
Learn more with the Python API reference.


Why is this awesome?

You can now share a parameterized function without doing any extra work. By telling Blockspring about your inputs, you automatically get a form so anyone can run your function manually. Learn more about input parameter types.


Wait - how do I run my function in other languages with inputs?

Check out running Python functions to get a refresher on running Blockspring functions. Here's the updated Node one-liner:

var blockspring = require("blockspring");

// call scraper (https://open.blockspring.com/blocks/c959e1c889c10478f118140fa39bb1d0)
blockspring.runParsed("ba24a71b96ca430f2e319c1fd908e1af", {
  url: "http://www.twitter.com"
}, function(res){
  console.log(res.params["text"]);
});

While we're at it, we can also call our function from Javascript. Check out this d3 wordcloud that uses our function:

You're running Python right from Javascript!


Now it's your turn.


Congratulations young grasshopper. You've learned to add a Python function to your Blockspring library. Write a function, get an application. Now go build!


Create a new function Work at Blockspring