How can I tell if IPython is running? - python

I have an IPython notebook. I have a long-running loop that produces no output in one of the code blocks. It's not this, but imagine it was this:
for i in range(100):
time.sleep(2)
I started the code block running a while ago, and now I can't tell whether it's finished, or whether it's still running.
All the IPython status bar says at the top is Last Checkpoint: 23 minutes ago (autosaved). There's nothing in the browser tab to show whether it's running code, either.
I don't want to start the next block because I don't know if this block has finished.
And I don't want to stop the kernel and add print statements to this block, because if it's 80% of the way through, I don't want to kill it and restart it!
Is there anything in IPython - either the browser window or the console - that indicates what code is running right now?

You can see whether code still being executed or not in three places,
In the top right corner, there will small circle which stays solid till the execution of code completes.
Every cell will have cell number but when that cell being executed it will show * instead of a number
In chrome and firefox browsers tabs icon changes from notebook to a clock.

The block of code will have the line number indicator like this ln[*], which means that the block is running:
Edit 1:
There is a bug in Jupyter notebooks and Jupyter lab (formerly known as Ipython notebooks) where refreshing or reopening the notebook will not keep the previous output (which includes ln[*]): https://github.com/jupyter/jupyter/issues/83
Edit 2:
If you are willing to use a library to show if a cell is running (and its progress), I recommend tqdm to add progress bars:
ASCII Bar:
GUI Bar:

look to the right of ipython terminal, there is a circle. if the block is still running you would see a solid circle and when you hover it says "kernel busy"
if there is nothing running, you would see an empty circle and when you hover it says "kernel idle"

Also, in the browser tab (chrome in my case), you see the timer icon when a cell is executing. This is handy if you are in some other tab and want to check the execution status.
timer image

Related

How to debug a JupyterDash app using VSCode?

This is going to be a self-answered post, but that only means that I've got an idea of a solution, and that I'm very eager to hear about more efficient and stable approaches to the challenge in question.
I would like to know how you can combine the powers of JupyterDash and VSCode in order to run a debugging process for an .ipynb file that involves all of the following steps:
stepping into code,
running code line by line,
inspecting variables,
setting breakpoints,
inspecting callbacks, and
interactively editing variables.
I believe I've tried every imaginable combination of:
Step into code with F10
Run > Start Debugging (F5) from the VSCode menu
Run and Debug (ctrl+Shift+D) from the Jupyter Notebook menu
Inspecting variables through JUPYTER:VARIABLES in the VSCode Debug Console
Still I don't feel I've found a work-flow that is 100% satisfactory when it comes to interactivity and stability. And perhaps this isn't the best approach at all? Suggestions that do not include JupyterDash are therefore more than welcome.`
System info:
Python 3.9.6
VScode 1.60.2
Plotly 5.1.0
JupyterDash 0.4.0
I'll base this suggestion on the code presented here to produce a basic dash app with an interactive figure and a dropdown that triggers a callback:
Part 1 & 2 - Stepping into code and running code line by line
By default for a an .ipynb file, F10 in VSCode will trigger Step into function and place a yellow arrow at the first line of a cell. You can then step through your code line by line by hitting F10 again.
Figure 1.1 - Stepping into code with F10
Part 3. inspecting variables
After stepping through df = px.data.tips(), you can find the variable under JUPYTER:VARIABLES in the Debug Console:
Figure 3.1 - Debug console and variables
You can also inspect the contents of df by clicking the icon highlighted above to Show variable in data viewer:
Figure 3.2 - Data viewer
Part 4 - Setting breakpoints
If you've got some data manipulation going on within a callback you can set a breakpoint inside the callback:
Figure 4.1 - Set breakpoint.
Part 5 - Inspecting callbacks
In order to make the process move on to that particular breakpoint, you can hit F5 or even trigger the callback through the Dash App that this code sample produces. For example by choosing aggsunset from the dropdown menu:
Figure 5.1 - Make a selection to trigger callback debugging
Now you should notice that nothing happens in the figure itself. And if you go back to VSCode you'll see that the debugging process has moved on to your breakpoint.
Figure 5.2 - Code halting a breakpoint after callback is triggered
As you can see, a new variable is defined withing the callback as df2 = df.copy(). Strange as it is though, now you can only see the local variables of the callback:
Figure 5.2 - Jupyter Variables while debugging
But one neat thing here is that you can inspect the input from the dropdown in your app, which is:
colorscale = 'agsunset'
What's even cooler is that every time you change your dropdown value and trigger the callback, the value is updated under JUPYTER:VARIABLES:
Section 6 - Interactively editing variables
Figure 6.1 - Changing variables while debugging callbacks
If you move on to the DEBUG CONSOLE tab, you can take a closer a look at df2 with df2.tail():
Figure 6.2 - Inspect new variables
And you can even add a new column with df2['new_var] = 1:

How to enable vs code terminal?

This may be a really basic question but I am not clear how to proceed. I have started using python in datacamp courses but now moving to my own terminal is giving me some issues. When I open VS code and open the file I want to work the terminal does not allow me to write, it just appears a path, I tried to run a simple code but it does not run, basically happen anything. Do you know if I have to do something specific before start running the code to enable the terminal?
import matplotlib.pyplot as plt
year = [1950, 1970, 1990, 2010]
pop = [2.519, 3.692, 5.563, 6.972]
plot1 = plt.scatter(year, pop)
plt.show()
And this is what I see on the screen, below the path highlighted in yellow the terminal does not allow to write
I Appreciate any suggestion, Thanks
The terminal is working fine, but there is currently a program running in it!
You called Python to execute your clase.py file. So, all input/output you do in the terminal will now go to/come from your Python program that you just called, not your shell.
You can either wait for the program to be finished (you will get another prompt then) or press Ctrl+C to abort your program. Since your program looks like it's outputting a plot somewhere, I'd assume it opens a new window for that - in this case, the terminal will return to the prompt once that window is closed.
If you need a second terminal to do something else while the program is running in the first, you can click the "+" button to open a second terminal (you can switch using the dropdown), or the "split" button right next to the "+" to open the second one at the side.

Did my python subprocess break something?

I'm learning python and playing with the subprocess module. After reading several different tutorials and descriptions of how to use the module, my understanding was still not incredibly solid so I started working up some simple code to test the various functions and see how everything worked.
I wanted to see how it might behave if I tried to start the same process multiple times, using this code:
import subprocess
def tpltest(x):
while x > 0:
try:
subprocess.Popen('wordgrinder')
except:
print 'something broke!'
x -= 1
x = raw_input('how many?')
tpltest(x)
When I ran this in my terminal, wordgrinder (terminal word processor) opened normally, but then the cursor advanced down the screen at a rate of about one line per second. It appeared to be movement only, rather than any characters being added to the blank file onscreen. I pressed ctrl+c to see if a wordgrinder remained after closing the first instance and was met with my regular command line prompt, but the cursor continued to scroll. I ran top and did not see extra wordgrinder instances open, but the cursor continued scrolling.
Then I discovered the oddest part. If I rolled my mouse scroll wheel up in the window, chunks of text appeared over the window's contents. Some of the chunks were commands I had entered in the console over the past week, some of them were commands I had entered in my python shell but not the regular terminal, and some of the text was my WiFi security information, including the plaintext WPA2 password for my network. Scrolling the mousewheel down had no effect but scrolling up would overwrite a few lines with new text, seemingly from other random places in my system. After closing the terminal window and opening a new one, things were back to normal.
I'm running Debian 8 64bit with XFCE and using the default terminal emulator.
What did my code do, and why?
The explanation that I can offer is that what subprocess does is to run a background process. Additionally, wordgrinder is a program that takes full control of the terminal session by capturing input from the keyboard (stdin) and controlling the command-line (stdout) in a very particular way.
When you ran wordgrinder in the background through your script, it caused some funky things to happen. That's pretty crazy that your WiFi password ended up getting displayed from your experiment.

PyCharm: How to tell if previously sent selection has completed execution at console?

I use Alt+Shift+E to send a selection of code from the editor to the (IPython) console. But, I am not able to tell when the code has completed executing, since the next prompt appears even though the previous code chunk might not have completed executing. So:
Either I have to try and send another selection to the console, and the editor warns me that the previous command has not completed running, or,
I have to try and enter something at the console, and if the results of the requested computation are not returned (print 2 + 2, say), then I know that the previous command has not completed execution.
Here is a screenshot to show what I mean:
Am I missing some feature that tells me that a selection sent to the console has not completed executing?
As an example, R will not show the next prompt until one chunk has finished execution.
It is not a pycharm feature, but your print statements will execute after the previous code is finished running, in a way, letting you know everything is finished (my programming instructor always would put print "Ready" at the end of everything for this reason).
>>> import time
>>> time.sleep(15)
>>> print "hello"
# 15 seconds later
"hello"
This is now resolved in the latest builds of PyCharm. Thanks to JetBrains for fixing this.

Python Notebook run automatically periodically

I have a script in a notebook browser that does some live data analysis, that I would like to run periodically by itself once every few seconds/minutes, rather than I having to do Ctrl+Enter all the time.
Is there any way to achieve this? Do I need any additional plug-ins, and which ones
Simplest way to do it:
import time
def periodic_work(interval):
while True:
#change this to the function you want to call, or paste in the code you want to run
your_function()
#interval should be an integer, the number of seconds to wait
time.sleep(interval)
To run once per minute, run a new cell with periodic_work(60) -- you should see a closed circle in the top right of the IPython Notebook that indicates the Kernal is busy. If/when you want to stop the live update, hit the Stop button (labeled Interrupt) on the menu bar and wait a second or so. To start it up again, run the cell that calls periodic_work again.
One way would be to make your cell a function. In another cell, just call time.sleep () in an appropriate loop. You could also have it check for the existence of some external resource to quit, if you don't want to loop an a priori known finite number of times.

Categories

Resources