iPython/ Jupyter notebook clear only one line of output - python-3.x

How can I print the status of a Jupyter notebook on the previous line? I think I'm looking for something like clear_output(), but for only a single line.
Sample code:
from IPython.display import clear_output
import time
print('This is important info!')
for i in range(100):
print('Processing BIG data file {}'.format(i))
time.sleep(0.1)
clear_output(wait=True)
if i == 50:
print('Something bad happened on run {}. This needs to be visible at the end!'.format(i))
print('Done.')
When this runs it gives the behavior of over-writing the previous status line, but both of the lines marked as important (with exclamation points and everything!) are lost. When this is finished the display just says:
Done.
What it should say is:
This is important info!
Something bad happened on run 50. This needs to be visible at the end!
Done.
This post suggests using clear_output() and then reprinting everything. This seems impractical because the amount of data I really tend to display is big (lots of graphs, dataframes, ...).
Here's two SO links about clear_output().
Is there a way to make this work that doesn't involve reprinting everything?

I got it woking using the update function of the display handle:
from IPython.display import display
from time import sleep
print('Test 1')
dh = display('Test2',display_id=True)
sleep(1)
dh.update('Test3')

This will do it:
import IPython
IPython.display.display_javascript(r'''
var el = document.querySelector('.output_text:last-of-type > pre');
el.innerHTML = el.innerHTML.replace(/(\n.*$)/gm,""); ''', raw=True)

This simple escape sequence trick can do the job most of the time. \n and \r can do a lot if used properly.
\r: (Carriage Return) (CR) returns the cursor to the beginning of the new line without moving to a new line.
\n:(Line Feed) (LF) moves the cursor to the next line.
import time
print('This is important info!')
for i in range(100):
print("\r"+'Processing BIG data file {}'.format(i),end="")
time.sleep(0.1)
if i == 50:
print("\r"+'Something bad happened on run {}. This needs to be visible at the end!'.format(i))
print("\r"+'Done.')

Related

How to Create a Cursor or Loading Indicator for an Infinite Loop

I am scraping current follow requests from Instagram. I have a main infinite loop that it making the requests and prints OK when it is done. I want to display an animated cursor or loading progress while it is downloading names.
while 1:
response = requests.get(IG_CFR_PAGE, headers=headers(""), params=params, cookies=cookies)
if response.status_code == 200:
cfr = response.json()
for entry in cfr["data"]["data"]:
#print(entry["text"])
usernames.append(entry["text"])
if cfr["data"]["cursor"]!= None:
params['cursor'] = cfr["data"]["cursor"]
time.sleep(1)
else:
break
else:
print(response.status_code)
print("Error in request .... aborting")
break
print("ok")
I looked for tqdm but it takes an iterable. In my case, I am just looping over JSON keys in line for entry in cfr["data"]["data"]: and so I guess can't use it. I just need suggestions as to know what should I use to indicate that this script is actually doing something. I just need suggestions or pseudocode is fine to send me in a right direction... the actual programming code is not needed as I will do that myself.
Thank you
As far as I'm aware, most functions that allow you to change the mouse cursor in Python are available only from various GUI modules - most of the popular ones, such as tkinter, PyQt5, pygame or others.
The problem is that most of these may only work when you've created a window of the GUI, which is probably unnecessary or not a nice idea if you're not using the same GUI, or any GUI for that matter. Even then, some may only take effect when the mouse pointer hovers over a certain widget in that GUI.
Note:
I've only (unsuccessfully) tried doing this with pygame.cursors before. It may be convenient because it even lets you create a custom shape with strings, or use a system cursor. But it displays a pygame.error: video system not initialized if you try doing this without having called pygame.display.init() first. I tried creating a window and setting a cursor, but it didn't seem to take effect.
After a quick google search for other ways to set an animated cursor, I came across this SO answer which might offer some insight if you're on windows.
Overall, using a terminal animation may probably be better and easier, so
this is an attempt at a basic answer for a loading animation in a terminal window :
(Of course, for an indefinite length of loop, it doesn't make sense to store a percentage completion etc, so this just animates at an arbitrary pace and repeats when it reaches the end)
i, w, d = 0, 20, 10000
while True:
# Do whatever
# No print statements except this last one
i = (i+1)%(w*d)
l = i//d
print("\r Processing... |" + " "*l+ "█" + " "*(w-l-1) +"|", end='')
i is used for iteration, w is the length of the bar, and d used to create some sort of 'delay', so that the bar doesn't change at every single iteration, but some slower (visible) speed
Edit: Important Note: The '\r' that resets the cursor position doesn't work in every terminal - it may still move to a new line for the next print() instead of the start of the same line - but this should most likely be fine in your system Terminal/cmd etc... May not be a good idea to run this in IDLE :P
Edit 2: Based on your comment, for a blinking indicator (using the same approach) -
i, d = 0, 300000
while True:
i = (i+1)%d
print("\r Working... " + ("█" if i < d/2 else " "), end='')

How to get rid of the error 'EOFError: EOF when using input() after having used sys.stdin.readlines() in a different function?

I am trying to replicate an Excel VBA macro to Python for the sake of learning a new programming language and I am stuck at a point which Google alone is not helping (I guess I do not even know what to look for). Could you please give it a try?
What I expect the program to do:
When running the user should be prompted with a few options and if the input is 0 then it should ask for a multi line input containing a full HTML source code from this website Steam Tools
After the input the user is expected to hit CTRL+D / CTLR+Z to confirm there is nothing else to add (I think the problem is here, maybe it is not able to "clear" the EOF error while using input() again?)
Then as an ouput the program should return the first 10 rows delimited by comma and create another input() to avoid the window to autoclose.
What the issue is:
When I run it from the desktop (double cliking the .py file) it autocloses withouth creating the last input().
When I run it from PyCharm it runs OK and the last input remains waiting for user action. However, it does dump an error like this:
File "D:/Stuff/_root/py/Steam/steam_cards_manager.py", line 51, in z_parse_tbody
input('\nCopy the program output and type Back:') EOFError: EOF when reading a line
Any feedback is appreciated as I don't know if I am doing things in an easy / effcient way.
I've upload my .py file and also a sample HTML to make it easier to replicate the issue, hope it helps.
https://github.com/brmenezewe/db
It turned out that I had to replace the CTRL+D shortcut by a "trigger" word that when sent via a single input() it breaks the While loop and joins the inputs previously received:
def z_input_lines():
lines = []
while True:
line = input()
if not line or line.lower() != "go":
lines.append(line)
else:
break
return "".join(lines)

hidding the runfile() command in Spyder

Is there a way to 'hide' the runfile() command so that it doesn't get displayed on the IPython console? It can get really annoying when the file has a long path since it displays the path twice:
runfile('C:/Users/One/Desktop/Training/Week1/Files/file1.py',wdir='C:/Users/One/Desktop/Training/Week1/Files/file1.py')
I agree with you, I hate having a cluttered IPython console .
I found a way while I was trying to find a solution to clear the old text in the console when starting a script:
Define an anonymous function (reference: https://python-forum.io/Thread-Difference-between-os-system-clear-and-os-system-cls) at the beginning of your script to clear the console from text
cls = lambda: print("\033[2J\033[;H", end='')
cls()
The annoying runfile (...) text section will disappear :)
(Spyder maintainer here) This is not possible in our current stable version (Spyder 4), sorry.
This is how I made Alexis' idea work reliably for me. My screen got updated by my code before cls() finished executing, consequently it was blanked over.
from time import sleep
def cls():
print("\033[2J\033[;H", end='')
sleep(0.1)
cls()

Making Python text green and using Spinning cursor - Newbies questions

I want to make my Python script file,when I "announce" something to the user, to be green like this:
How can this be done?I saw a script using this with sys.stdout.write but I dont understand how to use it, Im using a simple "print" commands..
Also, I would like to have the Spinning cursor spin as long as this command runs and only stops when this command stops(finishes):
print('running network scan')
output = subprocesss.check_output('nmap -sL 192.168.1.0/24',shell=True)
print('Done')
Any way to do that (unknown time until task is done)?
Im using a code suggested by nos here:
Spinning Cursor
So, about getting the terminal color to be green, there is a neat package called colorama that generally works great for me. To check whether the process is running or not, I would recommend using Popen instead of check_output, since the latter does not allow you to communicate with the process as far as I know. But you need to since you want to know if your subprocess is still running. Here is a little code example that should get you running:
import subprocess
import shlex
import time
import sys
import colorama
def spinning_cursor():
"""Spinner taken from http://stackoverflow.com/questions/4995733/how-to-create-a-spinning-command-line-cursor-using-python/4995896#4995896."""
while True:
for cursor in '|/-\\':
yield cursor
# Create spinner
spinner = spinning_cursor()
# Print and change color to green
print(colorama.Fore.GREEN + 'running network scan')
# Define command we want to run
cmd = 'your command goes here'
# Split args for POpen
args=shlex.split(cmd)
# Create subprocess
p = subprocess.Popen(args,stdout=subprocess.PIPE)
# Check if process is still running
while p.poll()==None:
# Print spinner
sys.stdout.write(spinner.next())
sys.stdout.flush()
sys.stdout.write('\b')
print('Done')
# Grab output
output=p.communicate()[0]
# Reset color (otherwise your terminal is green)
print(colorama.Style.RESET_ALL)

Python colorama not working with input?

Finally got colorama working today, and it works excellent when printing strings, but I got the common error everyone seems to get when I attempted to use colorama with input.
Here's my code:
launch = input(Fore.GREEN + "Launch attack?(Y/N): ")
Screenshot of output:
I had this same issue (Python 3.5.4) and, just in case it is not too obvious for somebody else looking at this, you can always rely on the workaround of combining print / input calls where you previously had just an input call:
print(Fore.GREEN + "Launch attack?(Y/N): ", end='')
launch = input()
This should produce the exact same output as in your question, with no extra blank lines and with the code coloring working without the need of importing anything else.
The (small?) disadvantage is that you you will end up with two lines of code where you previously had just one.
On my system, input() works with colors if you add
import sphinx.quickstart
to your module.
So here is the full code.
from colorama import Fore
import colorama
import sphinx.quickstart
colorama.init()
launch = input(Fore.GREEN + "Launch attack? (Y/N): ")
(This leads to two questions:
Why does it not work in the first place?
What is the actual reason? – Someone might like to dive into the sphinx source code.)
N.B. if you run python via winpty from Git Bash, set convert.
colorama.init(convert=True)
Otherwise, you do not get color with the current versions.
To get rid of this problem in the starting of the code add
import os
os.system('cls')
This will clear the screen and hence clear all the external factors blocking the colorama in input.
This works 100% you just need to do it once in the starting of the program [or just before the first use of colorama with input] and after that use colorama in any creative way you want to.
I hope this will help all the creative minds trying to make their codes more colourful
just make sure about the 'autoreset' in init()
init(autoreset=True)

Resources