How Does Notebook Print out Work?

08 Apr.,2024

 

Hi,
This may seem like a simple enough problem.
I am developing a panel dashboard, as a class. It has some functions inside, that read data from files and process them as per some selections based on widgets. Then it finally plots outputs on the graph.

My question is, how can I print something like I would when I am working on jupyter notebook, to the cell output, so that I can debug the intermediate steps? As I was developing the data processing steps, I had to debug some steps, but I could not know by any means what was being stored in my variables or at what step the processing was going haywire. Unless something absolutely breaks, I do not get even an error statement.

PS: I tried printing out stuff using a StaticText widget but my preprocessing involves pandas dataframes and dictionaries and such, which I cannot view properly in the output of a StaticText widget.
What I am trying to do is something like this:

Did you try putting the following code in the next cell after the one you had in the image and running that following executing the first cell?

main()

Your first code block declared & defined the function. Then you need to call the function. (More about main() here and here.)

Additionally, putting a simpler printf() in a cell alone and running that cell would have been a more direct way to test where the output for the printf() function will show.

Always share code as text and not an image, or at least not just an image. It makes it easy for those trying to help you test things and return to you a variation on your code. In this case, the image was indeed useful because it also indicated the specific C++ kernel you were using as launches via MyBinder service from here (click ‘launch binder’) offer three versions.

Because you mention being new to C++, you may want to go to the work through the demo notebook available when you press ‘launch binder’ at the site I referenced in the last section. (Click here to launch directly.) If you prefer to do it in C++17 & JupyterLab, when the classic interface opens, click on the Jupyter logo in the upper left side. That will switch to the JupyterLab interface. It will start it in what corresponds to the root of the repository and so to get to the demo notebook in the JupyterLab interface, double-click on ‘notebooks’ in the file browser panel on the left. Then double-click on ‘xcpp.ipynb’ listed there. When you are in the ‘xcpp.ipynb’ notebook in JupyterLab, click on the kernel indicator in the upper right to toggle a selector & change the kernel to ‘C++17’ so that it matches your kernel. Work through executing the cells.

Technical Supplement
Leaving C++ code here to be handy, if needed later:

#include <cstdio>
#include <iostream>
#include <string>

int main() {
    int x =5;
    double y = 3.14;
    printf("The value of x is: %d and the value of y is: %f\n",x,y);
    return 0;
}

How Does Notebook Print out Work?

How do I print a value on a console in a jupyter notebook?