Did you try it?
I just ran the following in one cell and it was fine.
%pip install pandas
import pandas as pd
(You can test in yourself by launching JupyterLab with an ipykernel here and trying the import statement first to verify it isn’t installed.)
Point is that it is not reliable for all packages, and so it is not the standard supported way.
I think you may be dragging out an XY Problem here?
Let me ask why would you want to do that with Jupyter? Why are you limited in cells? Why can’t you install it in one of the many ways you can install things in the environment so that it is pre-installed and not needed to installed in the same cell?
Py.cafe is awesome but it is designed very differently than Jupyter and the typical ipykernel.
JupyterLite is a bit closer in relation to it. In particular the jupyterlite-xeus-python allows you to pre-install packages, see here. And with the pyodide kernel you can do dynamic installing at this time, (I believe something along the line is in works for jupyterlite-xeus-python). For example, go to here, and click on the yellow ‘Try lite now badge’. Then when that comes up start a new notebook with the ‘Python (Pyodide)’ kernel. Then put a cell import ipywidgets as widgets
and it will say not installed with the error ModuleNotFoundError: No module named 'ipywidgets'
. And so now open yet another new notebook with the ‘Python (Pyodide)’ kernel and put the following in the first cell, based on Afonit’s code and adding the install command magic command (that as I said earlier, also works in JupyterLite):
%pip install ipywidgets
import ipywidgets as widgets
import pandas as pd
df = pd.DataFrame({'a': ['a', 'b', 'c'], 'b': [1, 2, 3]})
tab_contents = ['P0', 'P1', 'P2', 'P3', 'P4']
children = [widgets.Output() for name in tab_contents]
tab = widgets.Tab()
tab.children = children
tab.titles = [str(i) for i in range(len(children))]
# Here you are showing the dataframe in the output which resides in the tab
with children[0]:
display(df)
tab
It works on my machine in Chrome browser and so I hope it works on yours.
Finally, even closer to Py.cafe in some ways is Voici, that is JupyterLite combined with Voila for apps/dashboards based on Jupyter .ipynb
files, but last I knew it is still in very early development. You can try it by clicking here. That is the demo you can also access from the yellow badge here on the documentation page. One of the hurdles I found to developing with Voici as lack of easy preview like you get with Voila; however, the end of the post from the end of 2023 says that is in the works.