Using Python PyPI Packages

If you haven't gotten setup with the extension yet, start with the Quickstart Guide

One of Python's superpowers is no doubt the huge collection of third party packages that can be installed. These packages let your Python use functionality ranging from data analysis and machine learning to web development and automation. Neptyne also gives you access to all of this.

Neptyne comes bundled with many popular packages, but continue reading to learn how to install any pip installable package.

In this tutorial we'll convert emoji strings like thumbs_up to their corresponding emoji: 👍 using the emoji package.

From the Neptyne menu, click the Python Package Management button. In the box, paste the string: emoji. This will install that package and make it available in the editor and spreadsheet.

You can test out the package by running: emoji.emojize(":thumbs_up:") in the REPL in the bottom of the Neptyne sidebar.

Now that we have the package installed, paste the following code in the top right of your editor.

import emoji

def generate_emoji_string(emoji_name, amount):
    emoji_name = emoji_name or ""
    amount = amount or 0
    emoji_param = f":{emoji_name.lower()}:"
    e = emoji.emojize(emoji_param, language='alias')
    if e == emoji_param:
        return ""
    return "".join([e for i in range(amount)])

def count_emojis(cells):
    return sum([len(cell or "") for cell in cells])

This creates two new functions. The first, generate_emoji_string, can be used to generate a string of repeated emojis. Test it out in the REPL with: generate_emoji_string("cat", 5)

The core logic of the function simply uses the emoji.emojize function, but with some additional checks to return an empty string if you are missing a parameter or the emoji name isn't found.

Now we're going to use this function in our spreadsheet! Add headers in A1: Emoji Name, in B1: Amount, and in C1: Result.

In C2, paste the formula: =PY("generate_emoji_string", A2, B2)

Now that we have the function, try putting some values in A2 and B2 such as Fire and 3 respectively.

Now like normal spreadsheet formulas, we can drag down from the corner of C2 to spread this formula to nearby cells. Drag down to C4 to add this formula to C3 and C4, and then fill in some values in A3:B4.

=Py("generate_emoji_string", A2, B2)
A
B
C
D
1
Emoji Name
Amount
Result
2
Fire
3
🔥🔥🔥
3
Fish sticks
2
4
heart
4
❤️❤️❤️❤️
5
7
6

Finally, our last goal is to count the number of emojis in the C column. It's a little more difficult than summing the B column, because that doesn't take into account emojis that aren't found, like "Fish sticks".

Luckily we already have the count_emojis function! Call it in C5 by adding the formula =PY("count_emojis", C2:C4)

Notice that we can pass a range into our python functions as well! Cells will behave like a list and we can iterate through it. In this implementation, we used a list comprehension, but a for loop would have gotten the job done as well!

Now that you've experienced the power of adding python packages to your spreadsheet, continue to the Application Gallery to see more examples of how you can use python and spreadsheets across a huge variety of use cases.