Getting Raw python error for converted Jupyter Notebook code

Summary

I am trying to run to my Streamlit app a code that was converted from a Jupyter notebook to python. I am fairly confident that the code was converted successfully as I have used code previous converted from a Jupyter notebook and had no issues.

Steps to reproduce

add code here

import pandas as pd
import re


df1 = pd.read_excel("C:\\Users\\avida\\freeweibo-09-15-2022.xlsx")
#df.index.names = ['row_id']


df2 = df1[['User_name', 'time_created', 'OriginalPostLink', 'HotTerm', 'content']]
df2 = df2.dropna() # If column value is empty drop the entire row
df2['orig_freeweibo_row_id'] = df2.index

weibo_id_list = []

for ind in df2.index:
    row_content = df2['OriginalPostLink'][ind]
    weibo_id = re.findall(r'\d+', str(row_content)) or ["Error"]
#    print(weibo_id[0])
    weibo_id_list.append(weibo_id[0])
    
#print(weibo_id_list)
df2['weibo_ids'] = weibo_id_list

# Dropping rows that contain ['0'] as a weibo id
###df2 = df2[df2["weibo_ids"].str.contains("0") == False]
# Remove rows where content is 'NaN'

# df2.head()
print("Number of rows in original data: ", len(df1))
print("Number of rows after removing empty rows in columns:", len(df2))
print("Number rows removed:", len(df1) - len(df2))
print("Range of the index:", len(df2[df2['content'] != 2].index))
# df2['content'] != 2 subsets the df2, and len(df2.index) returns the length of the index.
# https://stackoverflow.com/questions/47539511/how-to-get-range-of-index-of-pandas-dataframe

df2.head()

If applicable, please provide the steps we should take to reproduce the error or specified behavior.

Explain what you expect to happen when you run the code above.

The expected behavior is for the code to print number of rows in original data, number of rows after removing empty rows in columns, number of rows removed and range of the index. In addition there is a generic table that is usually displayed in streamlit that displays the data.

Explain the undesired behavior or error you see when you run the code above.
If you’re seeing an error message, share the full contents of the error message here.

I received the error when running the code streamlit run filename.py
Usage: streamlit run [OPTIONS] TARGET [ARGS]…
Try ‘streamlit run --help’ for help.

Error: Streamlit requires raw Python (.py) files, but the provided file has no extension.
For more information, please see https://docs.streamlit.io

Streamlit version: 1.15.1

  • Python version: 3.9.15
  • Using Conda
  • OS version: Windows 10
  • Browser version: Chrome

Requirements file

Using Conda? PipEnv? PyEnv? Pex? Share the contents of your requirements file here.
Not sure what a requirements file is? Check out this doc and add a requirements file to your app.

Links

  • Link to your GitHub repo:
  • Link to your deployed app:

Additional information

If needed, add any other context about the problem here.

Could you edit your post to format your code block, please? Make sure your code block starts and ends with triple backticks (```). It’s much easier to help that way. :smile:

Fixed thank you didn’t realized I pasted it outside the backticks.

Thanks.

Are you sure you included the .py in the command streamlit run filename.py?

And is the above snippet being called directly or from another script? There are no Streamlit commands in the converted notebook file. Print statements go to console in a Streamlit app.

So i do have .py at the end of the filename does a streamlit command have to be in the code in order for it to work on Streamlit. This is juat straight python code so I at the very least wasn’t expecting the raw python code error.

I made a .py file that had a single print statement and called it test.py. Streamlit didn’t throw any errors per se when I used streamlit run test.py, and the line was printed to console. However, as expected it just generated a blank page in my browser since the print statements are only going to console and not the Streamlit page. Streamlit will render some variables and literals when they exist independently on a line, but that doesn’t apply to print statements.

The error I quoted above usually comes from trying to run streamlit run test instead of streamlit run test.py. I’m a littled stumped if the file is both saved as .py and you are including the .py in your streamlit run command…

Where’s the import statement for streamlit?