'Use own file extension in streamlit's file_uploader

Streamlit's file_uploader widget let's you define the allowed file extensions via the type option:

type (str or list of str or None)

Array of allowed extensions. ['png', 'jpg'] The default is None, which means all extensions are allowed.

In contrast to the common file extensions I am working with a non-standard one like .details.txt. These files can be selected if type='txt is configured:

uploaded_files = st.file_uploader(
    "Choose file(s)",
    type=['txt'],
    accept_multiple_files=True,
)

However, this also allows the user to provide other txt files which I want to prevent. The user should be able to upload .details.txt files only, but not files like .report.txt.

Specifying type='.details.txt' or type='details.txt' did not do the trick. In these cases I was not able to select any file.

The following snippet can be used to reproduce the behavior described:

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import streamlit as st

uploaded_files = st.file_uploader(
    "Choose file(s)",
    type=['txt'],
    # type=['.txt'],
    # type=['details.txt'],
    # type=['.details.txt'],
    accept_multiple_files=True,
)

In addition, you would need to create a something.txt and a something.details.txt file on your filesystem.

Is there a way to specify own non-standard file types?



Solution 1:[1]

The following works for me in Streamlit, version 1.6.0.

type=['.details.txt'],

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 ferdy