How to work with .prm, .dlg and .bin files with st.file_uploader?

Hey guys!
Hope everyone is doing great.
I need to upload different files extensions in my application, but Iā€™m facing difficulties with that. I need to work with .prm, .dlg and .bin files, but when I use st.file_uploader, it returns me type ā€˜application/octet-streamā€™.


I already tried to use io.BytesIO to read, but it doesnā€™t work :frowning:
Iā€™ll be grateful if someone could help me with this.

Hi @samaraprass -

Can you post the code you are trying to run?

In the case of octet-stream, thatā€™s the general mime type for arbitrary data that donā€™t have a more specific definition. Ultimately, it should work the same, as underneath itā€™s all bytes of data.

Best,
Randy

Hi, @randyzwitch! :smile:
I apologize for my superficial explanation and late response.
Iā€™m working on developing a Pylinac web application with streamlit. Pylinac is a Python library to analyze the images and datasets commonly used by therapy medical physicists in the course of their routine linac QA. One of itā€™s modules, Log Analyzer uses .bin and .dlg type of files. I ran the ā€˜Tlog.binā€™ file in a jupyter notebook and worked just fine with log_analyzer.TrajectoryLog class. But when I tried to use it with st.file_uploader returned this error message: ā€™ TypeError: expected str, bytes or os.PathLike object, not UploadedFile '.
The code screenshot and error message are below.




Thanks for clarifying!

When you get an issue about UploadedFile, thatā€™s because that is the class name we define within Streamlit. To make the data accessible to other Python programs, please see the Example section in our docs:

Because different libraries expect different sorts of data inputs, unfortunately we canā€™t make this portion more seamless.

Best,
Randy

1 Like

Thanks for your help @randyzwitch! :slightly_smiling_face:
Iā€™ve already tried reading the file, but then this happened (screenshot) and the app just couldnā€™t work. I believe it wonā€™t be possible to add this module to the app, unfortunately.

Hey @samaraprass

I recently helped someone facing a similar issue.
To solve it, instead of passing the file directly to the function, I first saved it to the disk using shutil library.
Here is a sample code for it:

if file is not None:
    # save the uploaded file to disk
    with open("Tlog.bin", "wb") as buffer:
        shutil.copyfileobj(file, buffer)
    tlog2 = log_analyzer.TrajectoryLog("Tlog.bin")

Let me know if this helps.

Best,
Kanak

3 Likes

Hi @Kanak!
Thank you so much for your help! :grin:
Worked perfectly.

1 Like

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.