Exit Jupyter Notebook .ipynb cell based on a condition












1















from sys import exit
import sys
i=0
while i < 10:
print(i)
i=i+1
if i==5:
exit(0)

print(' i do not want to print this')


Based on the above code snippet , I am looking to exit Jupyter Notebook cell.



Error I am getting is :



/usr/local/lib/python3.6/site-packages/IPython/core/interactiveshell.py:3275: UserWarning: To exit: use 'exit', 'quit', or Ctrl-D.
  warn("To exit: use 'exit', 'quit', or Ctrl-D.", stacklevel=1)


I tried



sys.exit(0) 


but still not able to exit the execution gracefully



An exception has occurred, use %tb to see the full traceback.

SystemExit: 0









share|improve this question























  • use quit() - The kernel appears to have died. It will restart automatically.

    – Rohit-Pandey
    Jan 2 at 16:49






  • 1





    Why would you want to do so? exit() normally would kill the kernel, not just the cell. If you simply want to bypass some piece of code, you can put it under a boolean flag. Something like: for i in range(10): i+=1, if i==5: flag= True and then outside of for loop: if not flag: print('this will be printed')?

    – Quang Hoang
    Jan 2 at 16:52











  • Introducing a flag variable is a good idea , I was trying to replicate the problem with a simple code snippet. As same code works in databricks

    – K.Pil
    Jan 2 at 23:13
















1















from sys import exit
import sys
i=0
while i < 10:
print(i)
i=i+1
if i==5:
exit(0)

print(' i do not want to print this')


Based on the above code snippet , I am looking to exit Jupyter Notebook cell.



Error I am getting is :



/usr/local/lib/python3.6/site-packages/IPython/core/interactiveshell.py:3275: UserWarning: To exit: use 'exit', 'quit', or Ctrl-D.
  warn("To exit: use 'exit', 'quit', or Ctrl-D.", stacklevel=1)


I tried



sys.exit(0) 


but still not able to exit the execution gracefully



An exception has occurred, use %tb to see the full traceback.

SystemExit: 0









share|improve this question























  • use quit() - The kernel appears to have died. It will restart automatically.

    – Rohit-Pandey
    Jan 2 at 16:49






  • 1





    Why would you want to do so? exit() normally would kill the kernel, not just the cell. If you simply want to bypass some piece of code, you can put it under a boolean flag. Something like: for i in range(10): i+=1, if i==5: flag= True and then outside of for loop: if not flag: print('this will be printed')?

    – Quang Hoang
    Jan 2 at 16:52











  • Introducing a flag variable is a good idea , I was trying to replicate the problem with a simple code snippet. As same code works in databricks

    – K.Pil
    Jan 2 at 23:13














1












1








1


1






from sys import exit
import sys
i=0
while i < 10:
print(i)
i=i+1
if i==5:
exit(0)

print(' i do not want to print this')


Based on the above code snippet , I am looking to exit Jupyter Notebook cell.



Error I am getting is :



/usr/local/lib/python3.6/site-packages/IPython/core/interactiveshell.py:3275: UserWarning: To exit: use 'exit', 'quit', or Ctrl-D.
  warn("To exit: use 'exit', 'quit', or Ctrl-D.", stacklevel=1)


I tried



sys.exit(0) 


but still not able to exit the execution gracefully



An exception has occurred, use %tb to see the full traceback.

SystemExit: 0









share|improve this question














from sys import exit
import sys
i=0
while i < 10:
print(i)
i=i+1
if i==5:
exit(0)

print(' i do not want to print this')


Based on the above code snippet , I am looking to exit Jupyter Notebook cell.



Error I am getting is :



/usr/local/lib/python3.6/site-packages/IPython/core/interactiveshell.py:3275: UserWarning: To exit: use 'exit', 'quit', or Ctrl-D.
  warn("To exit: use 'exit', 'quit', or Ctrl-D.", stacklevel=1)


I tried



sys.exit(0) 


but still not able to exit the execution gracefully



An exception has occurred, use %tb to see the full traceback.

SystemExit: 0






python jupyter-notebook ipython






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Jan 2 at 16:44









K.PilK.Pil

102111




102111













  • use quit() - The kernel appears to have died. It will restart automatically.

    – Rohit-Pandey
    Jan 2 at 16:49






  • 1





    Why would you want to do so? exit() normally would kill the kernel, not just the cell. If you simply want to bypass some piece of code, you can put it under a boolean flag. Something like: for i in range(10): i+=1, if i==5: flag= True and then outside of for loop: if not flag: print('this will be printed')?

    – Quang Hoang
    Jan 2 at 16:52











  • Introducing a flag variable is a good idea , I was trying to replicate the problem with a simple code snippet. As same code works in databricks

    – K.Pil
    Jan 2 at 23:13



















  • use quit() - The kernel appears to have died. It will restart automatically.

    – Rohit-Pandey
    Jan 2 at 16:49






  • 1





    Why would you want to do so? exit() normally would kill the kernel, not just the cell. If you simply want to bypass some piece of code, you can put it under a boolean flag. Something like: for i in range(10): i+=1, if i==5: flag= True and then outside of for loop: if not flag: print('this will be printed')?

    – Quang Hoang
    Jan 2 at 16:52











  • Introducing a flag variable is a good idea , I was trying to replicate the problem with a simple code snippet. As same code works in databricks

    – K.Pil
    Jan 2 at 23:13

















use quit() - The kernel appears to have died. It will restart automatically.

– Rohit-Pandey
Jan 2 at 16:49





use quit() - The kernel appears to have died. It will restart automatically.

– Rohit-Pandey
Jan 2 at 16:49




1




1





Why would you want to do so? exit() normally would kill the kernel, not just the cell. If you simply want to bypass some piece of code, you can put it under a boolean flag. Something like: for i in range(10): i+=1, if i==5: flag= True and then outside of for loop: if not flag: print('this will be printed')?

– Quang Hoang
Jan 2 at 16:52





Why would you want to do so? exit() normally would kill the kernel, not just the cell. If you simply want to bypass some piece of code, you can put it under a boolean flag. Something like: for i in range(10): i+=1, if i==5: flag= True and then outside of for loop: if not flag: print('this will be printed')?

– Quang Hoang
Jan 2 at 16:52













Introducing a flag variable is a good idea , I was trying to replicate the problem with a simple code snippet. As same code works in databricks

– K.Pil
Jan 2 at 23:13





Introducing a flag variable is a good idea , I was trying to replicate the problem with a simple code snippet. As same code works in databricks

– K.Pil
Jan 2 at 23:13












0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54010060%2fexit-jupyter-notebook-ipynb-cell-based-on-a-condition%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54010060%2fexit-jupyter-notebook-ipynb-cell-based-on-a-condition%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

MongoDB - Not Authorized To Execute Command

How to fix TextFormField cause rebuild widget in Flutter

Npm cannot find a required file even through it is in the searched directory