0

When using Jupyter code cells in VSCode (described here, using # %%) how does one force all output in a cell to be printed? I was doing that using for Jupyter notebooks.

from IPython.core.interactiveshell import InteractiveShell
InteractiveShell.ast_node_interactivity = "all"

Is there something similar here? For example in a cell try typing and running

pd.DataFrame({"a":[1]})
pd.DataFrame({"b":[2]})

It will only print the second dataframe. One can wrap print() around each of them and both are printed but then styling is lost (df.style.set_properties...).

ironv
  • 978
  • 10
  • 25
  • 1
    You don't want `print()` with dataframes, you want `display()`, I believe. See [the very bottom section here](https://stackoverflow.com/a/76007551/8508004) where I talk about printing multiple dataframes from code in one cell and add titles. In fact, if you use the IPython/Jupyter features meant to control this directly, you don't need to invoke `InteractiveShell.ast_node_interactivity = "all"` in your notebooks and you'd more easily have code that robustly worked in situations. YMMV in conjunction with the VSCode interactive window. I'm mainly try to help your Jupyter use with this comment. – Wayne Apr 18 '23 at 16:11
  • Thanks for your response. The reason I mentioned Jupyter was just to indicate that type of functionality. This question is about getting `display` for VSCode in the interactive cell environment. – ironv Apr 18 '23 at 16:14
  • 1
    Does this cell environment allow Matplotlib plots and HTML, then it probably allows the `display()` to work, too? If you think it works with 'df.style.set_properties'-stuff it should work. – Wayne Apr 18 '23 at 16:22
  • 1
    Thanks again. It does work -- I am not sure what was happening before...user error for sure. Thanks for your help. – ironv Apr 18 '23 at 16:35
  • is there a way to get `display` to work in a python script (and not just ipython)? The power of the VSCode functionality I linked above is one can debug like a Jupyter notebook but finally run it like a script. `display` does not work when run using python scriptname.py. – ironv Apr 21 '23 at 16:52
  • 1
    Can you use `ipython scriptname.ipy` where you are working? – Wayne Apr 21 '23 at 17:04
  • When I run it that way I get `` as the output for that dataframe. Input: `display(excel_df.style.set_properties(subset=(excel_df.index)`. Is there any way of actually showing the output in a log file? – ironv Apr 21 '23 at 17:16
  • 1
    I am unsure where exactly you are working and so I'm at a loss of what to suggest. If the console only allows text to run and doesn't handle output of certain types going to a handler, then you have to use what is allowed by pure Python and output handling in it. That being said, I did find a way to save dataframe representations as images from in a Python script, see about `df2img` [here](https://stackoverflow.com/a/69250659/8508004) and [here](https://stackoverflow.com/a/75513529/8508004). – Wayne Apr 21 '23 at 17:36
  • Did you try the import, like [here](https://stackoverflow.com/a/61365251/8508004). I suspect you did but it may help someone later. – Wayne Apr 21 '23 at 18:03
  • Yes, doesn't help. – ironv Apr 21 '23 at 19:41

0 Answers0