Home > database >  How to insert the user input for a Python code when I save it to html
How to insert the user input for a Python code when I save it to html

Time:01-23

I wrote a code in Python on Jupyter Notebook that works with a user input. Then, I saved it in an html file to share it with others but they can't change the inputs.

How can I make the html file interactive as the ipynb file?

On Jupyter I wrote var = input("What is your variable? ") and works well when running the code in Python.

Then I saved it to html using: jupyter nbcovert YourNotebook.ipynb --no -input --to html

CodePudding user response:

If you convert a notebook to HTML, it is no longer editable or runnable. It just stores the In/Out blocks as HTML elements. Send the .ipynb to your friends if you want them to be able to run code/interact with it.

CodePudding user response:

Iam206's answer is definitely the short version. You need to keep it in .ipynb form for interaction to be possible for what you want.

Long version:

Some types of interfaces used in conjunction with notebook do retain interactivity in HTML form following conversion from the notebook. Those are the ones like Plotly plots or some widgets that also have javascript underlying them. Not anything that needs active Python to run will be interactive at that point.

However, these days there are some options to serve live Python kernels in a browser to share active Python-backed Jupyter notebooks with others. If you put your Jupyter notebook in a public repository, you can point the MyBinder.org service at it and it have temporary session on a remote machine where no login is needed. To see it in action go here and press the launch binder badge. The session will spin up and open the notebook that the URL points at. You'll see it is an active notebook and you can run Python code. (Or even other language kernels.) Here's an example of how I adapted something along the lines of that example to allow others to use my Jupyter notebook. The computational ability is limited and the connection times out after like ten minutes of inactivity. The sessions are ephemeral and there's no way to get them back once they are gone, so save anything useful to your local computer. MyBinder is one the easiest ways to share your notebooks in active form. Of course, if you notebook cannot be public that is out.

These days you can serve a site of static files and let people use your notebook via the experimental, still-developing JupyterLite that actually runs Python inside the user's browser powered by WebAssembly (WASM). You can see it action by clicking the 'Retro' button in the upper left side of the screen there. After it launches on your computer, you can click on the JupyterLite icon in the upper left and it will open the Jupyter Dashboard and then you can click on the pyolite directory in the folder there and in there are many other demonstration notebooks. The JupyterLite Get Started page is where you want to look if you are curious how to deply JupyterLite.

input() with WASM works differently so you need to change the code. If you want to work with typical Python code, you'll want to say with MyBinder, or one of the alternatives mentioned below for a typical Python-backed kernel.

There are companies you can pay to host your notebook so others can login and run things with full,typical Python-backed kernels. Since most people have Google accounts, Google Colab is a general use one. Keep in mind Google Colab is an older, more specialized variant of Jupyter. So there are other Google Colab-like offerings. And you could host your own full JupyterHub if you really needed to share your notebooks and could afford it.


To add, input() is only meant for basic things/protyping. If you want to make it a better user interface down the road, you'd use ipywidgets or something similar to interact with a user and collect input.

  • Related