Skip to content
Tutorials Center
My learning

Topics

Lesson 13 of 38 ยท Input Widgets

Date, File and Media Input

Collect dates, times, files, photos and colors in Streamlit. Read an uploaded CSV with pandas, filter by a date range, limit file types and sizes, and handle bad files safely.

Apps become useful when they work with the user's own data: a date range for a report, a CSV to analyze, a photo to classify, a brand color to preview. Streamlit has widgets for all of these. The data arrives as ordinary Python objects (datetime.date, file-like objects, hex strings), and from there it is regular Python and pandas.

The Streamlit snippets are real and read-only, with the visible result described. The runnable blocks demonstrate the code that processes what the widgets return, using in-memory stand-ins for uploaded files.

Dates and times

Python
import datetime as dt
import streamlit as st

day = st.date_input("Report date", value=dt.date(2025, 6, 1))
start_end = st.date_input(
    "Period",
    value=(dt.date(2025, 1, 1), dt.date(2025, 1, 31)),
    min_value=dt.date(2024, 1, 1),
    max_value=dt.date(2025, 12, 31),
)
alarm = st.time_input("Alarm", value=dt.time(7, 30))

The first widget shows a text box with 2025/06/01 and a pop-up calendar. Its return value is a datetime.date. The second shows a calendar in which the user selects a start and an end; a tuple default turns on range mode. The third shows a time picker and returns a datetime.time.

Watch out: in range mode, until the user has clicked both ends the widget returns a tuple with only one date (length 1), not two. Unpack carefully:

Python
if len(start_end) == 2:
    start, end = start_end
    st.write(f"{(end - start).days + 1} days selected")
else:
    st.info("Pick an end date.")

Other practical points: format="DD/MM/YYYY" changes how the box displays the date; value=None starts empty; the widget never returns text, so you can compare and subtract dates directly.

Filter by a date range (runnable)

The matching pandas logic is a boolean mask on a datetime column. This block uses the same unpack-with-a-length-check pattern as the widget:

Python Filter rows by a date range

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Two rows fall inside the period, adding up to 185.75. See the pandas lesson on dates and times for more.

st.file_uploader

Python
import pandas as pd
import streamlit as st

uploaded = st.file_uploader("Upload a CSV", type=["csv"])

if uploaded is None:
    st.info("Waiting for a file.")
    st.stop()

df = pd.read_csv(uploaded)
st.write(f"{uploaded.name}: {len(df)} rows")
st.dataframe(df)

The page shows a drag-and-drop area with a "Browse files" button and the note "Limit 200MB per file". Until a file is chosen, uploaded is None and the info box appears. After uploading, the page shows the file name with the row count, and the table. The returned object (UploadedFile) behaves like an open binary file, and has useful attributes: .name, .type (the MIME type), .size (bytes), .getvalue() (all bytes) and .read().

Options:

  • type=["csv", "xlsx"] limits the choice by extension (checked in the browser and the server).
  • accept_multiple_files=True returns a list of files (empty when none).
  • The default size limit is 200 MB per file; raise or lower it with the config option server.maxUploadSize.
  • Uploaded files live in memory on the server for the session. They are gone when the session ends; save them yourself if you need to keep them.

Reading an upload safely (runnable stand-in)

Users upload messy files: the wrong format, an empty file, an odd encoding. A robust reader returns a clear message instead of crashing. The block below uses io.BytesIO objects as stand-ins for uploaded files, since both behave as file-like objects that pd.read_csv accepts.

Python Stand-in: read uploads with error handling

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

In the app: df, error = load_upload(uploaded, uploaded.name), then st.error(error); st.stop() if there is an error. One more trap: after reading a file with .read(), its cursor is at the end, so reading again returns nothing. Call uploaded.seek(0) first, or use getvalue(), which always returns everything.

Never trust the file name or contents. Do not use uploaded.name as a path on disk without cleaning it, and be careful about opening files such as pickles, which can run code.

Camera, audio and color

Python
photo = st.camera_input("Take a picture")          # needs browser permission
if photo:
    st.image(photo)

clip = st.audio_input("Record a message")          # WAV audio, after permission

color = st.color_picker("Brand color", "#4c72b0")
st.write("You picked", color)

camera_input asks the browser for permission to use the camera, shows a live preview, and returns the picture (a file-like object just like an upload) after "Take photo". audio_input records from the microphone. color_picker shows a swatch and a color panel and returns a hex string such as #4c72b0. These widgets need a secure context (HTTPS or localhost), so they work on localhost and on any normal HTTPS deployment but not on a plain-HTTP address.

Hex colors are easy to work with in Python. This runnable block converts the hex string a color picker returns into RGB and decides whether black or white text is easier to read on it:

Python Use the hex string from a color picker

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Widgets that return nothing yet

A common thread in this lesson: many widgets start with no value (None, an empty list, a tuple of length 1). Your script must handle that first run without crashing. The reliable pattern is:

  1. Create the widget.
  2. If its value is missing or invalid, show a helpful message and st.stop().
  3. Only then use the value.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does st.file_uploader return before the user chooses a file?

    It returns None (or an empty list with accept_multiple_files=True) until files are uploaded.
  2. 2. A date range input has only the start date clicked. What does it return?

    In range mode it returns a tuple whose length is 1 until both dates are chosen, so check len() before unpacking.
  3. 3. What does color_picker return?

    It returns a hex color string.
  4. 4. Why call uploaded.seek(0) before reading a file a second time?

    File objects keep a read position; seek(0) rewinds it. getvalue() always returns the whole content.

Summary

  • date_input returns datetime.date (a tuple in range mode, possibly of length 1); time_input returns datetime.time.
  • file_uploader returns None, one UploadedFile or a list; read it with pandas or getvalue(), and handle bad files with clear messages.
  • camera_input, audio_input and color_picker return files or strings; the first two need a secure context and browser permission.
  • Always handle the empty first run, then st.stop() until the input is valid.

Next: Forms and Callbacks