I would like to display an image and this is my code:
from PIL import Image
imagg = Image.open('FSnm.png')
st.image(imagg, width=None)
I tried linking the path to the image, but the same error would occur. I am confused because the image would just show when running the webapp in my localhost.
In this case, your code can’t find the image file to open. Have you tried specifying the exact location of the file in your code? The way you have it specified now, you are using a relative reference to your working directory, which might not be where the picture is actually located.
When you open a file with the file name , you are telling the open() function that your file is in the current working directory. This is called a relative path. If the user does not pass the full path to the file (on Unix type systems this means a path that starts with a slash), the path is interpreted relatively to the current working directory. The current working directory usually is the directory in which you started the program. A good start would be validating the input. In other words, you can make sure that the user has indeed typed a correct path for a real existing file, like this:
while not os.path.isfile(fileName):
fileName = input("No such file! Please enter the name of the file you'd like to use.")
Another way to tell the python file open() function where your file is located is by using an absolute path, e.g.:
I’m having the same issue. Was anyone able to figure out the structure of Streamlit? The file that I need to open is in the same directory as my Streamlit app, but it’s not able to find it with a relative path.
Hi, so I had a similar issue. My images are being imported just fine when I’m running the program on my localhost, but as soon as I push it to GitHub, it gives an error that the image couldn’t be found. The File path specified is correct since it’s working on the local host(I’m providing a relative path since the image is located in the same directory).
Your model isn’t loading because the specified filename is incorrect. Line 12 should read
model = pickle.load(open('price_model.pkl', 'rb'))
Note the correct file extension is .pkl, while the one in your app is .pickle.
Here’s your app with the changes:
import streamlit as st
import pandas as pd
import pickle
import string
from sklearn.ensemble import RandomForestRegressor
#load model:
model = pickle.load(open('price_model.pkl', 'rb'))
def main():
# Title
st.title('Hi there! Welcome to the Jamaican House Price Predictor')
# header
st.header('Fill out the following then press submit to get a rough estimate of what your house could be sold for!')
# no. of beds slider
beds = st.slider(label='Number of Bedrooms', max_value=10)
# no. of bathrooms slider
baths = st.slider(label='Number of Bathrooms', max_value=10)
#Total sqft slider
size = st.number_input(label='Total size of the house in Sqft', max_value=10000)
#region slider
location = st.slider(label='Kingston', max_value=20)
inputs = [[beds, baths, size, location]]
if st.button(label='Submit'): #button
result = model.predict(inputs)
updated_results = result.flatten().astype(int)
st.success('Your house could be listed at JMD{} million'.format(updated_results))
if __name__ =='__main__':
main()