Issue#1 description When trying to use subprocess to run a colab (.ipynb) file inside streamlit app.py, the webapp UI is rendering perfectly well, but on clicking the button it is expected to execute the ipynb file. But instead it is rendering the entire colab file’s html instead of executing the file.
I may be unfamiliar with the way you are going about this, but I always thought you needed something like nbconvert to execute an .ipynb file like that.
For example, this line inside of a streamlit app will execute the notebook. I made a little notebook called test.ipynb located in a files folder that creates a simple text file. As written this command executes the notebook (creating the text file during execution), then creates an html file of the same name containing the results.
If you want more options for how to run system commands from a python script, here’s a good reference:
I’m not entirely sure all of what you want to happen/display but if you provide some more information about your inputs/outputs/desired results I’m sure you could get some more refined clarification.
import streamlit as st
import pandas as pd
import subprocess
greeting = st.selectbox('Select a Greeting',['','Hi','Hey','Hello','\'Sup','Greetings','Howdy'])
name = st.text_input('Name')
if st.button('Submit'):
if greeting == '' or name == '':
st.write('Please fill in name and greeting')
else:
inputs = pd.DataFrame({'greeting':[greeting],'name':[name]})
inputs.to_csv('files/my_notebook_inputs.csv', index=False)
subprocess.run(['jupyter','nbconvert', '--execute', 'files/my_notebook.ipynb', '--to', 'html'])
with open('files/my_notebook.html','r', encoding='utf-8') as f:
result = f.read()
result = result.removeprefix('<!DOCTYPE html>')
st.components.v1.html(result, height=400, scrolling=True)
In a notebook (files/my_notebook.ipynb) I have:
import pandas as pd
inputs = pd.read_csv('my_notebook_inputs.csv')
inputs
greeting = inputs.greeting[0]
name = inputs.name[0]
print(greeting + ', ' + name + '!')
Hi @mathcatsand ,
Thanks for your detailed response.
However, i do not need to convert .ipynb to .html
All i need is to convert .ipynb file to .py file and invoke it’s execution from streamlit app by passing 2 text inputs to it.
I know that .ipynb created by jupyter notebook has an option to download the file as .py file, but unfortunately colab’s .ipynb file doesnt give us that direct option.
The main reason why i am even trying to convert this .ipynb file to .py file is because, when i run this .ipynb file from streamlit’s subprocess method, instead of executing the file, its html code is printed as output . But when i individually run the .ipynb file, it properly executes all lines of code.
I just rendered the html results as an example. You need not do that if you don’t want to see that. I was just trying to illustrate that running an .ipynb file (rendered or not) from command line needs nbconvert or something else to fulfill that conversion need.
You can use nbconvert to get .py instead of .html if you just want to convert it once then hand Streamlit the already converted file or have Streamlit reconvert it each time. The documentation for nbcovert should give you all the options, but let me know if you want the example modified.
Thanks for stopping by! We use cookies to help us understand how you interact with our website.
By clicking “Accept all”, you consent to our use of cookies. For more information, please see our privacy policy.
Cookie settings
Strictly necessary cookies
These cookies are necessary for the website to function and cannot be switched off. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms.
Performance cookies
These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us understand how visitors move around the site and which pages are most frequently visited.
Functional cookies
These cookies are used to record your choices and settings, maintain your preferences over time and recognize you when you return to our website. These cookies help us to personalize our content for you and remember your preferences.
Targeting cookies
These cookies may be deployed to our site by our advertising partners to build a profile of your interest and provide you with content that is relevant to you, including showing you relevant ads on other websites.