Reputation: 3
1st code block:
import logging
logging.basicConfig(level=logging.DEBUG)
logging.debug("debug1abc")
logging.info("info1abc")
logging.warning("warning1abc")
logging.error("error1abc")
logging.critical("critical1abc")
The output for this if run separately::
DEBUG:root:debug1abc
INFO:root:info1abc
WARNING:root:warning1abc
ERROR:root:error1abc
CRITICAL:root:critical1abc
2nd code block:
import logging
logging.debug("debug1xyz")
logging.info("info1xyz")
logging.warning("warning1xyz")
logging.error("error1xyz")
logging.critical("critical1xyz")
The output for this if run separately:
WARNING:root:warning1xyz
ERROR:root:error1xyz
CRITICAL:root:critical1xyz
Now if I run both of these cells in a in cell 1, and in cell 2 [of the same notebook], both the outputs would be same [which is the 1st code's output].[[EDIT: meaning 5abc outputs, 5 xyz outputs]]
How can I get both the outputs if I run in the same notebook and make the outputs visible too? I know we can "clear outputs" to have the 2nd cell to run without saving the previous outputs. But I want the outputs to be visible too in the cell output and get these different outputs for each cell.
Is it not possible at all ?
Unsure on how the clear_ouptut is done
Upvotes: 0
Views: 177
Reputation: 9964
%reset -f
to clear variables set in the global namespace of JupyterIf you truly want to clear variables as you mention in your later comment. See here for more about this.
This won't though clear the import stuff or logging settings. Read on for how to do that...
I think I get what you want now by your original post. It is a common need in doing Python development anywhere, and also comes up inside Jupyter given the persistence of the namespace between cells. I just think you aren't using the right search terms or aren't understanding the underlying issue.
The underlying issue is how imports are handled by Python and by extension, IPython and Python-based kernels run in Jupyter, which inherit a lot from Python.
Python imports things into the namespace when you call the import. If it sees the import again, it doesn't actually reimport because it thinks it already did that import step for you and so it is saving time and not messing things up your namespace by skipping doing the import again.
However, during development this often may not be what you want to have happen. If you edited the package or module in the interim, you want the new package loaded into Jupyter.
Your case is like this. You do the import and then set the logging state in the first cell. One way to reset things for your next cell in Jupyter would be to say you want logging reloaded so it's state is back to the default. So one option is to put a cell in between the two cells you have:
import importlib
importlib.reload(logging)
Now you'd have three cells. The two cells based on yours should do what you seek and act like you had run them separately in new notebooks with fresh kernels.
You can also add those two lines to your second cell and that I cover next.
I expand on this and illustrate it in this notebook viewable here.
Example to overcome this by adding two lines to your second block:
Modify your second block to add two lines first to tell Python you don't want the normal handling of import for the logging module, that you specifically want to reload logging
into the kernel. So your first code block stays the same:
import logging
logging.basicConfig(level=logging.DEBUG)
logging.debug("debug1abc")
logging.info("info1abc")
logging.warning("warning1abc")
logging.error("error1abc")
logging.critical("critical1abc")
Second code block:
import importlib
importlib.reload(logging)
import logging # optional now
logging.debug("debug1xyz")
logging.info("info1xyz")
logging.warning("warning1xyz")
logging.error("error1xyz")
logging.critical("critical1xyz")
Because of the reload step, you could potentially skip including the second import logging
line. I left it in because it is more in line with your original code block and is moot anyway.
Plus, in addition to doing things at the level of the import, you may have the option of resetting the logging.basicConfig
level, right? (I think this was the direction Jottbe was suggesting.) I didn't try this yet because the more general solution that will serve you better in developing in Python/IPython/ Python-based kernels in Jupyter is to know about reload.
Plus I don't know if this is compatible with what you really need to accomplish here.
Upvotes: 0