Loading LAS Files - list index out of range

Hi all,

I have been using Streamlit to build a LAS file explorer, however, I am encountering issues when I try to load the file in my app. LAS files are used in the Oil & Gas industry to store well log data and they are essentially ASCII files with a unique header on top. These files can be read using the lasio library: https://lasio.readthedocs.io/en/latest/

When the app is first run (either locally or on Heroku) and I select a file using the file_uploader, all appears well until I go to another page in the app to view the data. When I do this I get an IndexError: list index out of range. I have copied the traceback below for reference.

I then click on Browse again and reselect the file. When I do this, all appears to be fine when working with the uploaded file. It continues to work until I close the browser tab and reload the app using streamlit run app.py

I have tried to debug the code using debugpy and VSCode, but I am going around in circles with errors relating to ports (ECONNREFUSED and ERROR No 48) so I have been unable to debug and work out if the issue is in lasio or Streamlit or my code.

Would anyone be able to offer any advice or help on this issue please? It’s driving me round the bend a little :slight_smile:

My app is hosted here:

Live Code used by Heroku:

Data Example:
An example LAS file can be found in my testing branch for this repo under the data folder.

IndexError: list index out of range


File "/anaconda3/lib/python3.6/site-packages/streamlit/script_runner.py", line 332, in _run_script
    exec(code, module.__dict__)File "/Users/andy/Documents/Python_Projects/LASExplorer/app.py", line 50, in <module>
    las_file, well_data = load_data(uploadedfile)File "/anaconda3/lib/python3.6/site-packages/streamlit/caching.py", line 591, in wrapped_func
    return get_or_create_cached_value()File "/anaconda3/lib/python3.6/site-packages/streamlit/caching.py", line 575, in get_or_create_cached_value
    return_value = func(*args, **kwargs)File "/Users/andy/Documents/Python_Projects/LASExplorer/app.py", line 24, in load_data
    las_file = lasio.read(string)File "/anaconda3/lib/python3.6/site-packages/lasio/__init__.py", line 41, in read
    return LASFile(file_ref, **kwargs)File "/anaconda3/lib/python3.6/site-packages/lasio/las.py", line 77, in __init__
    self.read(file_ref, **read_kwargs)File "/anaconda3/lib/python3.6/site-packages/lasio/las.py", line 113, in read
    file_obj, self.encoding = reader.open_file(file_ref, **kwargs)File "/anaconda3/lib/python3.6/site-packages/lasio/reader.py", line 70, in open_file
    first_line = lines[0]

Hi @andymcd, welcome to the Streamlit community! I’ve got a bit to do today with the first day back and all, but I’ll try to debug this in the next few days for you. I’ve actually worked with LAS files in the past, what a gross format :laughing:


1 Like

Thanks Randy. That would be much appreciated. I hope your first day back hasn’t been too busy :slightly_smiling_face:

Unfortunately LAS files are everywhere in the oil and gas industry. They are ok when you know all the little quirks with them, but there are still surprises every now and then. I wasn’t sure if there was initially a problem with the encoding when using the file loader and passing it to lasio, but I managed to resolve that early on.

1 Like

Hey @andymcd -

Changing your code to add a single line fixed it for me:

def load_data(uploadedfile):
    if uploadedfile:
        uploadedfile.seek(0)  #RZ: reset buffer to beginning each time
        string = uploadedfile.read().decode()
        las_file = lasio.read(string)

        # Create the dataframe
        well_data = las_file.df()

        #Assign the dataframe index to a curve
        well_data['DEPTH'] = well_data.index
    return las_file, well_data

This is a bug/misfeature that we worked on correcting in later versions of Streamlit since it was confusing to people. If you want to use Streamlit 0.70 per your requirements.txt file, then add the line of code I posted above.

However, if you upgrade Streamlit to the newest version (0.73.1 as of this post), your app will work as-is (I tested this scenario locally as well).


Thanks very much Randy. I upgraded the version of Streamlit to 0.73.1 and it appears to be working now. :slight_smile:

1 Like