Combining Neptyne and Streamlit

Note: This tutorial requires enabling Advanced Features. Head here for more information.

Are you ready to build interactive user interfaces within your spreadsheets? In this example we're going to add a guestbook interface to a spreadsheet by combining Neptyne with Streamlit. Streamlit is a powerful platform which allows you to build user interfaces using purely Python. With Neptyne's integration, you'll never even need to leave google sheets!

Check out the Neptyne Guestbook to see the final result.

So how does this work? Let's start with the most basic streamlit app. We create a function guestbook and use the nt.streamlit decorator. nt.streamlit supports a few different options such as controlling the size of the panel, setting it's tile, creating a public url, and configuring it to automatically open.

Try pasting this code into your code panel, and click the run button above guestbook to preview the streamlit app. Since auto_open is true, when you refresh the page notice that this streamlit app now opens automatically!

import neptyne as nt
import streamlit as st
import datetime


@nt.streamlit(auto_open=True)
def guestbook():
    st.title("Guestbook")
    st.write("Welcome! Sign your name, and check the sheet to see who else has been here :)")

Now lets add some interactivity. We're going to use a streamlit form. Paste this at the end of your guestbook function.

with st.form("myform", clear_on_submit=True):
    name = st.text_input("Name", key="name")
    note = st.text_area("Note", key="note")
    date = datetime.datetime.now().strftime("%d-%B-%Y")
    submit = st.form_submit_button(label="Submit")

    if submit:
        st.write("Thanks " + name + "!")

The first part of this code creates a streamlit form. We then bind the values of of that form to name, note, date and submit. The last part of the code checks if submit is True. This only happens after the user clicks the submit button. You can test the app out now and will see that the app writes your name after your submission.

The first part of this code creates a streamlit form. We then bind the values of of that form to name, note, date and submit. The last part of the code checks if submit is True. This only happens after the user clicks the submit button. You can test the app out now and will see that the app writes your name after your submission.

Now let's tie it all together by writing back to the sheet. We'll add 2 more lines inside the if submit check

new_row = [name, date, note]
B3: D.insert_row(0, new_row)

Here we are creating a list with the name, date and note. The second part uses Neptyne's insert_row api to add it to the 0 index (beginning) of the cell range at B3:D. insert_row and append_row are very convenient ways to maintain growing lists within Neptyne. Tying it all together, the completed code should look like this.

import neptyne as nt
import streamlit as st
import datetime


@nt.streamlit(auto_open=True)
def guestbook():
    st.title("Guestbook")
    st.write(
        "Welcome! Sign your name, and check the sheet to see who else has been here :)"
    )
    with st.form("myform", clear_on_submit=True):
        name = st.text_input("Name", key="name")
        note = st.text_area("Note", key="note")
        date = datetime.datetime.now().strftime("%d-%B-%Y")
        submit = st.form_submit_button(label="Submit")

        if submit:
            new_row = [name, date, note]
            B3: D.insert_row(0, new_row)
            st.write("Thanks " + name + "!")

To finish off your guestbook, add some headers from B2:B4. To dive deeper into what's possible with streamlit, check out their docs, or continue for more tutorials!

Advanced Features Tutorials