2  An Introduction to Loading and Displaying Data

Streamlit apps are just like standard Python scripts in a lot of ways.

This means that most of the standard methods we are used to for loading in data files will work!

As people working with data, much of what we want to load in is likely to be is data in a tabular format, like an Excel file or Google Sheet.

Let’s start by loading in and displaying a simple csv dataset.

Tip

st.write() is a handy command.

When you pass a variable to it - like some data you’ve loaded in and saved as a Python variable - it will automatically work out a good way to display it.

Other functions that we talk about in later chapters give you more control over exactly how data or files are displayed - but st.write() can often be a useful starting point.

1import pandas as pd
2import streamlit as st

3st.title("Loading in a data file from a publically-accessible csv")

4url = 'https://files.catbox.moe/otqf4i.csv'
5dataframe = pd.read_csv(url)

6st.write(dataframe)

7st.write("This is some text")

8st.write(123)

9st.write("https://upload.wikimedia.org/wikipedia/commons/thumb/b/b6/Felis_catus-cat_on_snow.jpg/")
1
We are going to be loading in and displaying some tabular data that’s stored as a .csv file, so we need to load in pandas. We choose to use the standard alias ‘pd’ for it.
2
We then load in streamlit. The standard alias is st - this is useful as in more complex apps we’ll refer to the streamlit library a lot of times!
3
Next we add a title to our app. This will appear at the top of the page in large text. We pass the title to the st.title() function as a string.
4
We then pass in a url for a dataframe. Alternatively, we could point to a dataframe stored locally on our machine, just like with a normal Python script - we’ll talk more about how that would work when we deploy our app to the web in a later chapter.
5
We pass the url (or the local filepath) to our pd.read_csv() function - we could specify some additional arguments here if we wanted to.
6
We use the st.write() function to prompt streamlit to display the dataframe, passing the variable we stored the dataframe into to the function.
7
We can also pass in a text string to st.write() to make it display in our app.
8
This also works with numbers…
9
… and web links.

Let’s take a look at how this app will look in the live Streamlit app below.

Note

In later chapters, we will learn more about loading in data, including

  • loading in other data types, like images and videos
  • allowing users to upload their own data
  • ‘caching’ data to prevent it being reloaded unnecessarily
Tip

The focus of this book is on giving you the tools to create simple Streamlit apps. For this, we will mainly focus on using csv files that are stored on the web or locally, or are uploaded by users. In other cases, we will look at data that is generated by simply running the app, as in the case of discrete event simulation apps where the act of running it produces the data for the graphs and tables that we want to analyse!

However, in real-world usage, there may be some instances where connecting to a database may be required to allow automated access to data without requiring it to be passed to the app, rather than relying on intermediate exports to formats like csv from the database.

Connecting directly to a production database is certainly possible and can be done safely and securely - but beyond the scope of the first version of the book.

If this is your first time working with streamlit, it is highly recommended to work through the book first to learn the core concepts of streamlit apps. Building a proof of concept version of your app with a .csv export or dummy dataset can be done before worrying about the task of connecting to a data source - and often a good proof of concept will provide the motivation within your organisation to unblock the things that may make connecting to data sources difficult!

Streamlit does provide tools and guidance around connecting to data sources for apps that are going into production; to learn more about connecting to other data sources, like SQL databases, you can take a look at this page from the streamlit documentation.

This page gives an overview of how to connect to a range of different SQL database types, public and private google sheets, and more.