Python Forum
How to programmatically stop a program in Jupyter Notebook?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to programmatically stop a program in Jupyter Notebook?
#1
Hi all,

I just want the program to stop/end when it comes across this line.

First I tried quit() and that didn't work

I then tried exit() and that caused the kernel to die.

import sys, sys.exit() works by raising an exception.

Why is this such a difficult thing? I don't know enough about programming yet to understand why. I've seen many posts about it on SO, here, and elsewhere online. I'm not the only one who gets confused as to how to handle it.

Here's the code:

import pandas as pd
import sys

fran = pd.read_csv(r'C:\Users\Mark\Desktop\FrancesMsg.csv',header=None)

lang_lib = {}

print(fran)
sys.exit()
print("exit didn't work")

for line in fran:
#    line.lstrip()
    line = str(line)
    print(line)
Reply
#2
What do you mean exactly? JuypterLab (or Notebook if you're still using the older version) are meant for interactive use. You just run the cells in your notebook that you want and not any of the others.
Reply
#3
(Aug-06-2021, 02:34 PM)ndc85430 Wrote: What do you mean exactly? JuypterLab (or Notebook if you're still using the older version) are meant for interactive use. You just run the cells in your notebook that you want and not any of the others.

Are you just saying I should cut lines after 8 if I just wanted to run that portion of it? That would work here.

I've done other programs, though, where I may want the program to stop right there if a condition occurs. A simple case would be "Continue? (Y/N) " If they enter Y, then quit. This may even be in the middle of a program so I wouldn't just paste a portion of the program in the cell to run. I'd want to test the whole thing in one Jupyter cell.
Reply
#4
Why do you want to run everything in a single cell? I suppose the point of the cells is to imitate the REPL you'd normally have, i.e. that you choose when to run the next thing. Jupyter does have a way to run all the cells (IIRC, there's a button for that) and may have a way to run only a subset of them (though I'm not sure about that).
Reply
#5
(Aug-06-2021, 02:46 PM)ndc85430 Wrote: Why do you want to run everything in a single cell? I suppose the point of the cells is to imitate the REPL you'd normally have, i.e. that you choose when to run the next thing. Jupyter does have a way to run all the cells (IIRC, there's a button for that) and may have a way to run only a subset of them (though I'm not sure about that).

Maybe I need to read about what's good about Jupyter Notebook and how it should be used. I'm just starting out with it (and I like it very much). If you have a link on it then I'll read that.

Given my current state of ignorance, though, I don't know why I can't use Jupyter Notebook either/or: either just a few steps at a time or the whole thing at once. If this were another IDE like Spyder, though, then I'd be running the whole program at once. I would then be asking the same questions about how best to stop a program in cases where it needs to be stopped. And I can imagine plenty of cases where a program would reach and endpoint and need to be stopped.

So why is this so complicated? How best to do it? Like I said, quit() didn't work, exit() caused the kernel to quit, which isn't really what I want, and sys.exit() stopped with an Exception raised, which isn't user-friendly. Thanks!
Reply
#6
Another option is wrapping in a try/except block and raising an exception. You could just raise without the try except if you dont mind seeing the traceback
try:

    for i in range(5):
        print(i)
        if i == 3:
            raise UserWarning('Exit Early')
    print('foobar')
except:
    pass
Reply
#7
(Aug-06-2021, 05:38 PM)jefsummers Wrote: Another option is wrapping in a try/except block and raising an exception. You could just raise without the try except if you dont mind seeing the traceback
try:

    for i in range(5):
        print(i)
        if i == 3:
            raise UserWarning('Exit Early')
    print('foobar')
except:
    pass

The exception is fine for me since I'm doing this for debugging purposes. It's not user-friendly, though, and there are plenty of instances where this should be useful--the most common being "do you wish to continue?"

This isn't an issue if it's the end of the code because nothing comes next and the code ends. If it's in the middle of the code then I see a void without some sort of statement that can do this. Also, AFAIK Python isn't like some other languages (BASIC, VBA) where you can have a "Goto" line that takes the program to the end for such purpose.

I feel like there's an important concept regarding all this that I have yet to learn.
Reply
#8
I get what you are saying, but structured programming languages such as Java, C++, and Python have avoided the "goto" statement of earlier languages. Reduces the spaghetti code.

As was mentioned, Jupyter (named for Julia, Python, and R, the 3 languages it was designed for) is designed for more interactive code execution - write a snippet, debug it, move to the next snippet, with each snippet taking care of a task. Great for data exploration. Takes some thought to organize the snippets so it breaks where you want, not so great for programmatic flow control like "skip the next 2 cells and start there". For that you are better off with one of the other IDEs, such as VS Code, Spyder, PyCharm, etc.

So it is choosing your tool. I just did a data analysis looking at mask and social distancing compliance in different populations. Did that in Jupyter Lab. I also have a project using Markov chains and that I am doing in VS Code. VS Code has introduced the ability to do notebooks so may look at that later, but bottom line is no one tool is best for everything.
Reply
#9
Lots of good details there. Thanks jefsummers!
Reply
#10
Just import 'exit' from the code beneath into your jupyter notebook (IPython notebook) and calling 'exit()' should work.
You can learn about Python string sort and other important topics on Python for job search.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  how come i can't stop program Shi_Shi_Shi_Shi 4 614 Nov-16-2023, 06:20 PM
Last Post: deanhystad
  My code works on Jupyter Lab/Notebook, but NOT on Visual Code Editor jst 4 1,012 Nov-15-2023, 06:56 PM
Last Post: jst
  Navigating file directories and paths inside Jupyter Notebook Mark17 5 710 Oct-29-2023, 12:40 PM
Last Post: Mark17
Question How create programmatically variables ? SpongeB0B 6 1,321 Aug-19-2023, 05:10 AM
Last Post: SpongeB0B
Thumbs Up Python 3 Jupyter notebook ternary plot data nicholas 0 940 Jan-21-2023, 05:01 PM
Last Post: nicholas
  Create Excel Line Chart Programmatically dee 3 1,190 Dec-30-2022, 08:44 PM
Last Post: dee
  Graphs from Jupyter notebook to word Scott 0 886 Nov-28-2022, 06:16 AM
Last Post: Scott
  Opening an empty Jupyter notebook in VSCODE Krischu 3 1,838 Mar-09-2022, 01:57 PM
Last Post: Larz60+
  How to do line continuation in Jupyter Notebook? Mark17 4 5,488 Sep-22-2021, 04:22 PM
Last Post: ibreeden
  Jupyter Notebook as IDE bytecrunch 0 1,608 Sep-12-2021, 07:41 PM
Last Post: bytecrunch

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020