Subprocess is not executing Colab ipynb file

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.

Code snippet:

Issue #2 - How can I pass the 2 text inputs captured from streamlit web app UI, and pass it to the subprocess python file as input parameters?

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.

I need to call and execute ipynb file from within streamlit app.py file. I am not sure how nbconvert is helpful here

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.

subprocess.run(['jupyter','nbconvert', '--execute', 'files/test.ipynb', '--to', 'html'])

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. :slight_smile:

I made a bit more of an example.

On a Streamlit page I have:

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 + '!')

Then I get something like this:

Example hosted here: https://mathcatsand-examples.streamlit.app/run_jupyter
(edit to change app url)

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.

1 Like

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.

1 Like

Oh I get your point now. I appreciate your patience in clarifying my doubt. I will read more about nbconvert and solve my problem. Thanks again!

1 Like

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