A Streamlit component to select files and folders from Google Drive using the official Google Picker.
This component lets you embed the Google Drive file picker directly in your Streamlit app, allowing your users to select files or folders from their Google Drive and work with them as Python file-like objects.
- Pick files or folders from Google Drive
- Multi-select support (pick multiple files) (pick folder in progress)
- Filter by file type or MIME type (e.g. PDF, images, etc.)
- Native Streamlit feel, like
st.file_uploader
pip install streamlit-google-pickerFor latest dev, use pip install git+https://github.com/LounesAl/streamlit-google-picker
You will need a Google Cloud project with proper OAuth 2.0 and API setup in the google cloud console:
- Create an OAuth2 Client ID (Web application ! Important)
- Set Redirect URI (e.g.
http://localhost:8501for local) - Create an API key then Enable both Google Drive API and Google Picker API
Set these in your environment:
GOOGLE_CLIENT_ID=your-client-id.apps.googleusercontent.com
GOOGLE_CLIENT_SECRET=your-client-secret
GOOGLE_API_KEY=your-api-keyAfter the user is authenticated (OAuth2, see examples), use the picker:
import streamlit as st
from streamlit_google_picker import google_picker
token = st.session_state["token"]["access_token"] # From your OAuth2 flow
CLIENT_ID = os.environ.get("GOOGLE_CLIENT_ID")
API_KEY = os.environ["GOOGLE_API_KEY"]
APP_ID = CLIENT_ID.split("-")[0]
uploaded_files = google_picker(
label="Pick files from Google Drive",
token=token,
apiKey=API_KEY,
appId=APP_ID,
accept_multiple_files=True,
type=["pdf", "png", "jpg"], # file extensions or MIME types
allow_folders=True,
nav_hidden=False,
key="google_picker",
)
if uploaded_files:
for uploaded_file in uploaded_files:
st.write(f"Filename: {uploaded_file.name}, Size: {uploaded_file.size_bytes}")
# To get file content:
data = uploaded_file.read() # This downloads the file on-demand!
# Display or process as needed
st.write(f"Bytes: {len(data)}")Folder selection: (In Progress)
If the user selects a folder, all files inside (recursively) are returned as UploadedFile objects.
You can also use it with a single file (returns a single object or None):
uploaded_file = google_picker(accept_multiple_files=False, ...)
if uploaded_file:
st.write(uploaded_file.name)
content = uploaded_file.read()- If
accept_multiple_files=True, returns a list ofUploadedFileobjects (like Streamlit’s). - If
accept_multiple_files=False, returns a singleUploadedFileorNone.
Each UploadedFile behaves like a Python file object (subclass of io.BytesIO):
uploaded_file.name # Original filename
uploaded_file.size_bytes # File size in bytes
uploaded_file.type # MIME type
uploaded_file.url # Direct Google Drive URL
uploaded_file.id # File ID
uploaded_file.read() # Reads bytes (downloads on demand)A typical flow:
- User authenticates via Google OAuth2 (e.g. with streamlit-oauth)
- Store
access_tokeninst.session_stateand DB + auto refresh token (Here we use secrects.json for simulation) - Pass token to
google_picker() - Get the selected files as
UploadedFileobjects.
See example.py for a full sample.
- Clone this repo
- Install backend (Python) and frontend (React) requirements
- Run
npm install && npm startinfrontend/for hot-reload - Develop Streamlit component as usual
Found a bug or have a feature request? Open an issue!
