Jupyter: disable restart kernel warning - python

I'm using jupyter 4.1.0, and I find myself making frequent use of the "Restart & Run All" feature. Every time I use that button it displays this warning:
Is there a way to disable that warning?

You can add a cell in your notebook and using the following statements:
from IPython.core.display import HTML
HTML("<script>Jupyter.notebook.kernel.restart()</script>")
And the kernel will restart immediately.

To answer the question that was stated: No, there isn't.
But you might make your voice heard over here so they might implement it some day.

You can create a bookmark in your browser for the following code:
javascript:Jupyter.notebook.execute_all_cells();void(0);
Clicking this bookmark runs all cells without asking for confirmation.

Related

Is there a way to reopen a running JupyterLab tab with a output stream?

If I run
import time
for i in range(999):
print (i)
time.sleep(1)
close the tab and reopen, it wont show the live output stream.
I checked, its running in the background.
is there a way to come back to a running script after I closed the tab?
video demo:
https://imgur.com/hRSxEdh
First thing first: code doesn't stop on tab closing (and you can see it from the log if you are launching JN from Anaconda Prompt). This is a good hint to find a viable solution.
Actually there are many ways to do this, and I'll suggest to dig this very good answer for a lot of other methods.
To me the most Jupyter Notebook-ish solution is to use the cell magic %%capture.
How to use it:
%%capture temp_output
# your running script here
Save the Notebook, close the tab, do whatever you want.
Go back to the notebook, open it and:
temp_output.show()
Will let have your result.

Jupyter notebook input() not showing after using IPython display(markdown())

I am trying to build a Chatbot using Jupyter Notebook on Google Colab. I am using IPython Markdown to better segregate the user conversations. User will key in their message using input() while the bot responses are IPython display(markdown()).
I am facing issues when i tried to print a starting messages (bot output before a user input). This error occurs approximately 50% of the time I run the script. No errors are faced if I did not print out the starting message. I have tried restarting the kernel but to no avail.
Does anyone know what is the reason for this and how I can fix it?
This is an example of a successful case.
This is an example of a failed case. As you can see, the code is running indefinitely on the input() line.
This is the sample code, where input() box does not pop up at times.
from IPython.display import Markdown, display
display(Markdown("*Alex:* Hello."))
input(">> You: ")
This seems to be a kind of bug in Jupyter notebook, described in the past years by many programmers. I was facing the same problem. After several tests I have found (at least in my code) that the problem disappears if print() functions are removed before input().

Is there a way to autohide/autocollapse code cells after first execution in jupyter notebook?

I want to be able to autohide code cells after they have run once in a jupyter notebook.
I am calling a function that prints some output. For example
print("Hello World")
The problem I am trying to solve is that after the output is printed, the code block stays there. I can use ctrl k + ctrl o to hide the code block after execution however there are many code blocks like that and I want it to be automatic. I tried using some javascript (https://habr.com/en/post/439570/) however that didn't work as expected. I am aware that I can use NBconvert to hide all the code cells while generating a pdf however I want only some cells to be hidden and that to in the raw notebook.
I looked into how we can add metadata in the cell to hide it with python here: https://jupyterbook.org/content/metadata.html
However, I want to do it locally on some cells, and I want to prevent file changed, do you want to overwrite your notebook prompts.
Is this possible?
For Vscode, refer to the official documentation https://code.visualstudio.com/docs/datascience/jupyter-notebooks, however I couldn't find anything related to autohide/collapse when using Vscode. They might release this functionality in their next release.

how to clear the screen in pycharm

I know these are questions: this and this and this. I have tried all of these methods in pycharm, but they either a) not work or b) I get a weird character on my screen, that looks like a 0 with diagonal line going through it. I can't copy it for some reason, so I can't show you.
For instance this:
import os
from time import sleep
print("lots of text")
sleep(5)
os.system('cls')
What should happen in the above is tht the message should be displayed for 5 seconds then disappear, but all I get is that weird character... In contrast when I ran my python program from command line, it worked as expected i.e. the screen cleared. I am very confused.
So my questions are:
Why don't any of them work in pycharm?
What does work in pycharm?
Ok, so, I have been having this problem as well — after scouring the internet, the os commands work BUT you need to enable the terminal emulator.
In the PyCharm toolbar, click Run, Edit Configurations, and the toggle for "Emulate Terminal in Output Console" should be under Execution in the pop up. Emulate Terminal toggle
I had to get help from the PyCharm team on this one, so here's to them responding at lightning speed.
Answer
The cls command doesn't work on a standard pycharm project because it is an IDE and certain os commands don't work in the same way it would with the windows command line.
There is currently no way to clear the console output in PyCharm, other than to manually do it by using the cls command through the terminal in PyCharm or through a keyboard shortcut set through the pycharm preferences menu. You can try to emulate a keyboard shortcut, but this would be difficult, and unless you absolutely need it, I would not recommend clearing the terminal output after a program is run.
References and Additional Links
https://teamtreehouse.com/community/clear-screen-for-pycharm-as-if-it-were-on-console-or-cmd
I don't know in which version of PyCharm this was introduced. I guess it must have been after this question was asked.
The way to clear the PyCharm Python Console is by using the context menu. Right mouse click in the upper part of the console (where the lines with >>> prompts are closer together) and select Clear All.
Alse see this PyCharm Support Community article.
Incidentally, none of the questions linked to in the OP's original post relate to PyCharm and do not answer the OP's question.
cls only temporarily clears it. Next command brings back all previous messages. The best option is to right click on the Terminal and choose Clear Buffer

Plotly + iPython Notebook - Plots Disappear on Reopen

When I create a notebook with plotly plots, save and reopen the notebook, the plots fail to render upon reopening - there are just blank blocks where the plots should be. Is this expected behavior? If not, is there a known fix?
Check to make sure your notebook is marked as "trusted", either in the top-right corner of the notebook, or in the File menu.
My notebooks are trusted by default, but I managed to reproduce your observed behaviour by temporarily removing my ~/Library/Jupyter/nbsignatures.db file, which forces Jupyter to run the notebook as untrusted. Clicking the tile in the top-right corner to trust the notebook fixed the issue for me.
This page in the jupyter docs, Security in notebook documents explains further:
The security problem we need to solve is that no code should execute just because a user has opened a notebook that they did not write. Like any other program, once a user decides to execute code in a notebook, it is considered trusted, and should be allowed to do anything.
I also meet this annoying issue. I find no way to reveal them on the notebook, but I find a compromise way to display them on an html page that is File -> Print Preview.
The reason as documented here:
Note: Default renderers persist for the duration of a single session,
but they do not persist across sessions. If you are working in an
IPython kernel, this means that default renderers will persist for the
life of the kernel, but they will not persist across kernel restarts.
As indicated in the troubleshooting documentation, "JupyterLab Problems" section, You have two options to solve the problem:
calling fig.show("notebook") instead of just fig.show().
If this problem is recurrent, you can use this:
import plotly.io as pio
pio.renderers.default='notebook'
Credits for here

Categories

Resources