Quickstart: Run any Blockspring function with Python.

In this tutorial, we'll learn how a single line of Python gets you access to all of the powerful utilities, algorithms, API calls, visualizations (and more) in the Blockspring community library.

Installation


First, install Blockspring. From your command line, enter:

pip install blockspring

Blockspring is installed. To initialize it, open your Python environment and enter:

import blockspring

Your first run: get stock quotes.


Let's get historical stock quotes from Yahoo Finance in one line of Python:

res = blockspring.runParsed("historic-stock-prices", {
  "ticker": "MSFT",
  "start_date": "2014-1-1",
  "end_date": "2014-1-2"
})

You'll see the following quotes printed in your Python console:

print res.params["historical_quotes"]
[{u'volume': u'30632200', u'symbol': u'MSFT', u'high': u'37.40', u'low': u'37.10', u'trade_date': u'2014-01-02', u'close': u'37.16', u'open': u'37.35', u'adjusted_close': u'36.17'}]

You ran your first Blockspring function. Let's explain how that worked.


Blockspring functions in a nutshell


When you ran Blockspring.runParsed, here's what happened:

  1. Blockspring used your function name to find Get Stock Quotes, a simple call to Yahoo Finance, written in Ruby, and submitted by user pkpp1233.

  2. Get Stock Quotes quickly executed in the cloud, with your stock ticker, start date, and end date passed through as a input arguments.

  3. You got back results and printed the definition, found in params["historical_quotes"].

That instant replay should raise some questions. Let's answer them.


A quick Q+A


Where do I find more Blockspring functions?
Browse the community library to find utilities, algorithms, API calls, visualizations, and more. You're now a quick search away from programming just about anything.


Great, I found a function. How do I run it?
Click "Integrations" on the function's homepage, choose Python, and copy and paste the given code snippet to wherever you need it. Try it yourself with Get Stock Quotes.


How do I know what inputs my function requires?
Again, click Get Stock Quotes, go to "Integrations", click Python, and you'll see in the given code snippet the required inputs are stock ticker, start date, and end date. The snippets are helpful for finding a function's required input arguments.


How do I know where to find the function output?
The output is found in the result's res.params. Explore that variable to find the output you're looking for. To learn more about the response, visit the api reference.


How do I create my own Blockspring function?
Visit the next Python quickstart to learn how to add your own function to Blockspring.


Your second run: interactive google map


Let's create an interactive Google map in one line of Python.

res = blockspring.runParsed("interactive-google-map", {
  "locations": [
    ["Latitude", "Longitude", "Tooltip"],
    [37.4217542234, -122.100920271, "Somewhere"],
    [41.895964876906, -87.632716978217, "Out"],
    [28.58230778, 77.09399505, "There"]
  ]
})

Now print out the response.

print res.params["map"]
"/var/folders/6w/920995kj30d_tn8skl8ml5wr0000gn/T//RtmpYMBrLW/165f6491e48ac-map.html"

This function saves the interactive Google map as an HTML file to a temporary directory on your machine, and sets that location to res.params["map"]. That means you can open map as you would open any HTML file.

Go to your terminal and copy/paste the filepath you saw in res.params["map"] (remember - your filepath will be different) like so:

# remember - your path will be different!
open /var/folders/6w/920995kj30d_tn8skl8ml5wr0000gn/T//RtmpYMBrLW/165f6491e48ac-map.html

You should see an interactive map open in your browser that looks like the image below:

Python quickstart gmap

Your third run: summarize a URL


This time, let's summarize a URL in a line of Python.

To use Blockspring in your apps, you'll want to . Doing so guarantees you 100k function runs per month for free! now and we'll show you how to include it in your function below.

We'll also enable caching. If the summarization was done within the last 3,600 seconds, you'll receive it right away instead of needing to re-run the function.

res = blockspring.runParsed("summarize-text", {
  "url": "http://www.usatoday.com/story/life/music/2015/04/18/rock-hall-of-fame-2015-induction-ceremony/25913423/",
  "sentences": 1
}, {
  "cache": True,
  "expiry": 3600
})

print res.params["summary"]
# Such was the case Saturday night at the 30th annual gathering, which saw the inductions of Ringo Starr, Bill Withers, Joan Jett and the Blackhearts, Stevie Ray Vaughan & Double Trouble, the Paul Butterfield Blues Band, Green Day, Lou Reed and the "5'' Royales.

Become a Blockspring pro


Blockspring lets you program just about anything with a single line of Python and you're well on your way. You now know how to:

You can also add your own public (or private) functions to Blockspring. Any function you add to Blockspring can be easily run across languages, chained, composed, cached, and shared with others, without any set up or maintenance. Check out the next quickstart tutorial to learn how to create your first Blockspring function.


Discover new functions Work at Blockspring