Skip to main content

A Python package for creating subscription Streamlit apps

Project description

Releases

Streamlit App

Author: @tylerjrichards

Kind consultant: @blackary

Installation

pip install st-paywall

See it in action

Basic example: https://subscription.streamlit.app

 

🥟 st-paywall

A python package for creating paid Streamlit apps with a paywall!

Why st-paywall?

I made st-paywall so data scientists and LLM developers can create small businesses around their Streamlit apps. Every week I see dozens of new incredible apps built in Streamlit that are adored by users, but eventually shut down or moved off of Streamlit as authentication and payment integration are too hard. This is my attempt at a dead-simple API around each, abstracting the both away into a single function (add_auth). Enjoy!

Overview

This package gives you one basic function (add_auth) that adds subscription functionality to your Streamlit apps. add_auth will add both a Google login button if they are not logged in, and a Stripe subscription button to your sidebar if they are not subscribed. If they are subscribed, st.session_state.user_subscribed will be true, and if they are logged in, st.session_state.email will have their email. If the required parameter is True, the app will stop with st.stop() if the user is not logged in and subscribed. Otherwise, you the developer will have control over exactly how you want to paywall the apps!

I hope you use this to create tons of value, and capture some of it with the magic of Streamlit.

This package expects that you have a .streamlit/secrets.toml file which you will have to create. Inside it, you will need to add your Stripe and Google API information that runs the authentication and subscription parts of the package.

Stripe

In order to set up your Stripe (the best and easiest payment infrastructure in the world), go to Stripe.com and make an account. Once you make an account, you need to make a payment subscription link (which is in test mode by default) and add the link to your app (currently held in streamlit_app.py). If the user to your app logs in and has not already signed up and paid via Stripe, they will be asked to subscribe before they can see the rest of the app.

The subscription link should be added to secrets.toml like this.

stripe_link = 'https://buy.stripe.com/test_...'

You also need to create an Standard API key, which just like subscription links have test options as well. Store your API key in your secrets file as stripe_api_key and you're off to the races. If you make a restricted API key instead of a Standard key, make sure to add read permission on Customers

The API key should be added to secrets.toml like this

stripe_api_key = 'sk_...'

By default this repo links to creating test subscription links and test api keys (you probably already noticed the 'test' in the Stripe dashboard, the subscription link, and in our example api key). When you launch your app and want folks to pay real money, you will need to create production links and api keys from your Stripe dashboard and use those instead. While you are testing out the Stripe part of your code, you can use Stripe's test cards instead of inputting your own credit card info! To run st-paywall in test mode, add the following to your secrets file.

testing_mode = true

Then, the package will look for the following secrets. Otherwise, it will look for the production api and link secrets. I highly encourage you to start out in test mode!

stripe_api_key_test = 'sk_test_...'
stripe_link_test = 'https://buy.stripe.com/test_...'

Once you decide to leave test mode and publish your subscription app, it is a good idea to have Stripe send the user back to your app once they've subscribed. To do this, go to the payment links page, edit your payment link, click 'After payment' and fill out your app's url.

 

Google

In order to set up your Google Oauth, you need to register your web app with Google's OAuth system! So head over to this url and create a web application (and a project, if this is your first go around with Google Cloud). Give a unique name, and add 'http://localhost:8501/' as an "Authorized redirect URI" (when you deploy your app, you'll also need to add the final url of your app to this list). Now press create, and store your client id, and client secret in the secrets.toml file as client_id and client_secret.

For example:

client_id = '1234.....googleusercontent.com'
client_secret = 'GOC...'
redirect_url = 'http://localhost:8501'

The last step for your Google Oauth provisioning is to head over to the consent screen and edit what users will see when logging in. Fill out all the info they ask for, and make sure to add the email scope (called '.../auth/userinfo.email' by Google with the user description 'See your primary Google Account email address'). That should be it! If you are in testing mode, st-paywall will look for the following secrets.

client_id = '1234.....googleusercontent.com'
client_secret = 'GOC...'
redirect_url_test = 'http://localhost.com:8501'

For my test application at subscription.streamlit.app, my Google console looks like this.

Buy Me A Coffee

Not everywhere in the world is covered by Stripe! Buy Me A Coffee is a lighter, less business-y service that allows folks to support you online. To link to your buy me a coffee page instead of your Stripe payment link, go to their dev page and create an access token. You will also add a membership product offering as well. Once you have the membership link and the key, add the following to your secrets file.

payment_provider = "bmac"
bmac_api_key = "eyJ0...."
bmac_link = "https://www.buymeacoffee.com/..."

This will change your app to link out to buymeacoffee instead of Stripe, and will get your customer list from there as well!

Feedback:

If you have feedback about this package, please reach out to me on twitter or file an issue in this repo and I will do my best to help you out.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

st-paywall-0.1.3.tar.gz (8.9 kB view hashes)

Uploaded Source

Built Distribution

st_paywall-0.1.3-py3-none-any.whl (7.3 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page