<commit_before>---
layout: article
title: "The Python Client"
excerpt: "Get going with the Python client on Algorithmia."
permalink: /application-development/client-guides/python
redirect_from:
  - /clients/python/
  - /application-development/guides/python/
date: 2016-07-08
categories: clients
tags: [clients]
ignore_sections: [install-from-source, upgrading-from-0-9-x, running-tests]
show_related: true
image:
    teaser: /language_logos/python.png
---

This guide provides a walk-through of how to use the official Algorithmia Python client to call algorithms and manage your data
through the Algorithmia platform.

## Getting Started with Algorithmia

Install from PyPi

The official Algorithmia python client is available on PyPi. Install it with pip:

{% highlight python %}
pip install algorithmia
{% endhighlight %}

Authentication

First, create an Algorithmia client and authenticate with your API key:

{% highlight python %}
import Algorithmia

apiKey = '{Your API key here}'
# Create the Algorithmia client
client = Algorithmia.client(apiKey)
{% endhighlight %}

Now you’re ready to call algorithms.

## Getting your Data from the Data API

[Algorithmia's Data Portal](http://developers.algorithmia.com/application-development/data-sources/) offers three different ways to store your data, all available via the [Data API](http://docs.algorithmia.com/). 

This guide will show how to work with the [Hosted Data Source](https://algorithmia.com/data/hosted) option.

To start, you can either upload your data either through the UI in the [Hosted Data Source](https://algorithmia.com/data/hosted) or via the [Data API](http://docs.algorithmia.com/).

**Note:** Before you upload your file onto Hosted Data Source, you must first create a [Data Collection](http://developers.algorithmia.com/application-development/data-sources/hosted-data-guide/)

### Uploading Data to your Data Collection

{% highlight python %}
# Upload local file
client.file("data://username/data_collection_name/jack_london.txt").putFile("/your_local_path_to_file/jack_london.txt")
{% endhighlight %}

**Note:** Instead of your username you can use '.my' when calling algorithms.

### Downloading Data to use in Algorithm
{% highlight python %}
# Download contents of file as a string
input = client.file("data://username/data_collection_name/jack_london.txt").getString()
{% endhighlight %}

## Calling algorithms

{% highlight python %}
# Create the algorithm object using the Summarizer algorithm
algo = client.algo('nlp/Summarizer/0.1.3')
# Pass in input required by algorithm
result = algo.pipe(input)
{% endhighlight %}

**Note:** a single algorithm may have different input and output types, or accept multiple types of input, so consult the algorithm’s description for usage examples specific to that algorithm.


<commit_msg>Finished first draft of new client docs for Python<commit_after>---
layout: article
title: "The Python Client"
excerpt: "Get going with the Python client on Algorithmia."
permalink: /application-development/client-guides/python
redirect_from:
  - /clients/python/
  - /application-development/guides/python/
date: 2016-07-08
categories: clients
tags: [clients]
ignore_sections: [install-from-source, upgrading-from-0-9-x, running-tests]
show_related: true
image:
    teaser: /language_logos/python.png
---

This guide provides a walk-through of how to use the official Algorithmia Python client to call algorithms and manage your data
through the Algorithmia platform.

## Getting Started with Algorithmia

Install from PyPi

The official Algorithmia python client is available on PyPi. Install it with pip:

{% highlight python %}
pip install algorithmia
{% endhighlight %}

## Authentication

First, create an Algorithmia client and authenticate with your API key:

{% highlight python %}
import Algorithmia

apiKey = '{Your API key here}'
# Create the Algorithmia client
client = Algorithmia.client(apiKey)
{% endhighlight %}

Now you’re ready to call algorithms.

## Working with Data Using the Data API

[Algorithmia's Data Portal](http://developers.algorithmia.com/application-development/data-sources/) offers three different ways to store your data, all available via the [Data API](http://docs.algorithmia.com/). 

This guide will show how to work with the [Hosted Data Source](https://algorithmia.com/data/hosted) option on the Algorithmia platform. 

**Note:** If you wish to follow along working through the example yourself, create a text file that contains any unstructured text such as a chapter of a public domain book or article. This will be used throughout the guide.

To start, you can either upload your data either through the UI on [Hosted Data Source](https://algorithmia.com/data/hosted) or via the [Data API](http://docs.algorithmia.com/).

**Note:** Before you upload your file onto Hosted Data Source, you must first create a [Data Collection](http://developers.algorithmia.com/application-development/data-sources/hosted-data-guide/).

### Uploading Data to your Data Collection

Once you've created your data collection and assuming you haven't already uploaded your file via the Hosted Data Source UI, you're ready to do so using the Data API.

First create variable that holds path to data collection and name of text file you will be uploading:

{% highlight python %}
text_file = "data://your_username/data_collection_name/your_text_file.txt"
{% endhighlight %}

**Note:** Instead of your username you can use '.my' when calling algorithms. For more information about the '.my' pseudonym check out the [Hosted Data Guide](http://developers.algorithmia.com/application-development/data-sources/hosted-data-guide/).

Now upload your local file to the data collection using the .putFile() method:

{% highlight python %}
# Upload local file
client.file(text_file).putFile("/your_local_path_to_file/jack_london.txt")
{% endhighlight %}

### Downloading Data to use in Algorithm

{% highlight python %}
# Download contents of file as a string
if client.file(text_file).exists():
	input = client.file(text_file).getString()
{% endhighlight %}

## Working with Algorithms

In this guide we are using the natural language processing algorithm called [Summarizer](https://algorithmia.com/algorithms/nlp/Summarizer). If you are interested in learning more about working with unstructured text data check out our guide [Introduction to Natural Language Processing](http://blog.algorithmia.com/introduction-natural-language-processing-nlp/).

{% highlight python %}
# Create the algorithm object using the Summarizer algorithm
algo = client.algo('nlp/Summarizer/0.1.3')
# Pass in input required by algorithm
try:
	response = algo.pipe(input).result
except:
	print(algo.pipe(input).error.message)

{% endhighlight %}

This algorithm results in a text output that is the summary of article or book.

**Note:** a single algorithm may have different input and output types, or accept multiple types of input, so consult the algorithm’s description for usage examples specific to that algorithm.


## Conclusion

This guide covered installing Algorithmia via pip, uploading and downloading data to and from a user created Data Collection, checking if a file exists using the Data API, calling an algorithm, and handling errors.

For more information on the methods available using the Algorithmia Python Client check out the [Data API](http://docs.algorithmia.com/) documentation.

For convenience, here is the whole script available to run:

{% highlight python %}
import Algorithmia

client = Algorithmia.client("your_algorithmia_api_key")

text_file = "data://your_username/data_collection_name/your_text_file.txt"
client.file(text_file).putFile(
    "/your_local_path_to_file/your_text_file.txt")
if client.file(text_file).exists():
    input = client.file(text_file).getString()
algo = client.algo('nlp/Summarizer/0.1.3')

try:
    response = algo.pipe(input).result
    print(response)
except:
    print(algo.pipe(input).error.message)
{% endhighlight %}

{'fraction_non_alphanumeric': 0.09341002876078529, 'fraction_numerical': 0.0036263598849568587, 'mean_word_length': 4.959761549925484, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 5, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 5, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7040031', 'n_tokens_mistral': 2260, 'n_tokens_neox': 2161, 'n_words': 854}