python auto clears everything on screen after 298 lines - python

my problem is that python.exe auto clears everything ive printed to it, after a certain amount of lines has been reached, for example i created the following program
a = 0
for a in range(0, 10000):
print(str(a) + ':> cola')
delay = input('BLARG :>')
now when i ran this in python .exe, i got the following result.
IMAGE1, and it carried on like this till it reached the end. IMAGE2 now the problem is python erased everything in the first image that came before the 9701st print which is a tad troublesome, does anyone have any advice on how to prevent python auto clearing everything.

This is not a python problem. change settings of your console ~v1k45
Windows' CMD you're using have a limit of old output it remembers.
I don't have Windows in English, so I do translation of names of the options myself, and they can differ from reality.
To change the limit, right-click on it's title bar and select Properties (for current window, or Defaults to change it for all future cmd windows), go to the tab Layout and increase heigth of Buffer size:

Related

how to update stdscr size with python curses after resizing the terminal

I tried searching everywhere on the internet, but nowhere did I find an answer that worked for me, so I'm asking a new question.
In my code, I'm telling curses to get the terminal size and update some variables according to it. On my first try, I did something like this (this is the function that gets called after the window has been resized manually):
def resize():
rows, cols = stdscr.getmaxyx()
#some more code irrelevant to the question
When I found out that doesn't work, I realised that the stdscr is probably not getting resized, even though the terminal is. So I tried closing the stdscr and initialising it again:
def resize():
curses.endwin()
curses.wrapper(initialise)
#some more code that's irrelevant to the question
#showing the initialise function just in case:
def initialise(stdscrarg):
global stdscr
stdscr = stdscrarg
That, however, also didn't work. In other words, the stdscr initialised in the size that the terminal had before resizing. i.e. if I had a terminal over a quarter of my screen and then resized it to full screen, the function would correctly deal with the warping of the text, but it would redraw the content adjusted to just the original quarter of the screen, and would leave the junk characters in the rest of the screen, like this:
Before resizing
After resizing, before refreshing
After refreshing.
As you can see, the stdscr has initialised to its original size instead of taking on the size of the terminal. And I have tested it and confirmed that it really is the size of the stdscr, and not just the text printed on it (as when I gradually add strings to it, it throws an error when it reaches the end of the original size screen, instead of continuing to the end of the terminal screen and only then throwing an error.)
My question is: How can I get the new size of the terminal and make stdscr initialise with this new size?
Working on Windows 11, Python 3.10, module curses (though when installing it through pip, its name is windows-curses)

How to clear pycharm output terminal with code? [duplicate]

Is there a way to clear the "Run" console in PyCharm?
I want a code that delete/hide all the print() made previously.
Like the "clear_all" button, but without having to press it manually.
I have read that there is a way to do it in a terminal with os.system("cls"), but in PyCharm, it only adds a small square without clearing anything.
Also, I don't want to use print("\n" *100) since I don't want to be able to scroll back and see the previous prints.
In Pycharm:
CMD + , (or Pycharm preferences);
Search: "clear all";
Double click -> Add keyboard shortcut (set it to CTRL + L or anything)
Enjoy this new hot key in your Pycharm console!
Pycharm Community Edition 2020.1.3
You can right click anywhere above the current line on the console, and choose the "Clear All" option. It'll clear the console
How to
Download this package https://github.com/asweigart/pyautogui. It allows python to send key strokes.
You may have to install some other packages first
If you are installing PyAutoGUI from PyPI using pip:
Windows has no dependencies. The Win32 extensions do not need to be
installed.
OS X needs the pyobjc-core and pyobjc module installed (in that
order).
Linux needs the python3-xlib (or python-xlib for Python 2) module
installed.
Pillow needs to be installed, and on Linux you may need to install additional libraries to make sure Pillow's PNG/JPEG works correctly. See:
Set a keyboard shortcut for clearing the run window in pycharm as explained by Taylan Aydinli
CMD + , (or Pycharm preferences);
Search: "clear all"; Double click ->
Add keyboard shortcut (set it to CTRL + L or anything)
Enjoy this new hot key in your Pycharm console!
Then if you set the keyboard shortcut for 'clear all' to Command + L use this in your python script
import pyautogui
pyautogui.hotkey('command', 'l')
Example program
This will clear the screen after the user types an input.
If you aren't focused on the tool window then your clear hot-key won't work, you can see this for yourself if you try pressing your hot-key while focused on, say, the editor, you won't clear the embedded terminals contents.
PyAutoGUI has no way of focusing on windows directly, to solve this you can try to find the coordinate where the run terminal is located and then send a left click to focus, if you don't already know the coordinates where you can click your mouse you can find it out with the following code:
import pyautogui
from time import sleep
sleep(2)
print(pyautogui.position())
An example of output:
(2799, 575)
and now the actual code:
import pyautogui
while True:
input_1 = input("?")
print(input_1)
pyautogui.click(x=2799, y=575)
pyautogui.hotkey('command', 'l')
Easy Method:
Shortcut: Control K,
Right click on terminal and clear Buffer
There's also another way of doing it using the system class from os. All you need to do is have this code:
from os import system, name
# define our clear function
def clear():
# for windows the name is 'nt'
if name == 'nt':
_ = system('cls')
# and for mac and linux, the os.name is 'posix'
else:
_ = system('clear')
# Then, whenever you want to clear the screen, just use this clear function as:
clear()
However, in order for this functionality to work in pycharm, you need to enable "Emulate terminal in output console". You can find this under edit configuration of the file where you want to use the clear function, then it's under Execution option. Here's a screenshot: pycharm screensho
You could just do a ("\n" * 100000000), so it'll be impossible to scroll back.
In PyCharm terminal you can type 'cls' just like in linux terminal.
For Python Console (where you see the output) assign a shortkey for "clear all" in File -> Settings -> Keymap -> Other -> "Clear all"
You can also click somewhere on the PythonConsole -> Right button -> clear.
Hope it helps
I just relised that instead of going to the trouble of setting up a shortcut, you could just set up a command using PyAutoGUI to click on the trash bin on the side of the window e.g
note, to install pyautogui click on the end of the import pyautogui line, then press alt+enter and click install pyautogui.
import pyautogui
# to find the coordinates of the bin...
from time import sleep
sleep(2) # hover your mouse over bin in this time
mousepos = pyautogui.position() gets current pos of mouse
x,y = mousepos # storing mouse position
print(mousepos) # prints current pos of mouse
# then to clear it;
pyautogui.click(x, y) # and just put this line of code wherever you want to clear it
(this isn't perfect thanks to the time it takes to run the code and using the mouse, but it is reasonable solution depending on what you are using it for.)
I hope this answer is helpful even though this is an old question.
Just click the trash can icon to the left of the command window and it clears the command history!
In PyCharm 2019.3.3 you can right click and select "Clear All" button.This is deleting all written data inside of the console and unfortunately this is manual.
Sorry to say this, here the main question is how to do it programmatically means while my code is running I want my code to clear previous data and at some stage and then continue running the code. It should work like reset button.
After spending some time on research I solved my problem using Mahak Khurmi's solution https://stackoverflow.com/a/67543234/16878188.
If you edit the run configuration you can enable "emulate terminal in output console" and you can use the os.system("cls") line and it will work normally.
Iconman had the easiest answer.
But simply printing "\n" * 20 (or whatever your terminal height is) will clear the screen, and the only difference is that the cursor is at the bottom.
I came here because I wanted to visually see how long each step of a complex process was taking (I'm implementing a progress bar), and the terminal is already full of scrolling logging information.
I ended up printing ("A" * 40) * 20, and then "B" and "C" etc., and then filming it. Reviewing the video made it easy to see how many seconds each step took. Yes I know I could use time-stamps, but this was fun!

Executing multiple lines of input in pycharm console from first line

When working with the interactive console in PyCharm (via IPython), is there any way to execute the currently loaded multiline statement from not the last line?
In Jupyter's QtConsole, this can be done by pressing Shift+Enter on any line of input. i.e.
In [1]: print("line 1")
... print("line 2")
Can be evaluated by pressing Shift+Enter regardless of where the caret is located. In the IPython-based interactive console in PyCharm, pressing Shift+Enter creates a new line below the current one and moves the caret to it, while Ctrl+Enter splits the line at the current caret position.
I could not find any settings that control this behavior; perhaps it is a limitation of IPython?
EDIT: In the regular IPython REPL, Alt+Enter accomplishes what I'm after, but this does not work in the PyCharm console, even after removing the default shortcut assigned to that keystroke ("Show Intention Actions")
Perhaps this link can help you:
https://www.jetbrains.com/help/pycharm/loading-code-from-editor-into-console.html
pressing Alt + Shift + E on a selection will execute the selected code in the console.
If there is no direct shortcut for this, at least you can do it with a workaround:
Position to start with (call it PositionEND, since it is the end of the selection that you must paradoxically start with): Put the cursor in the end position up to which you want to run your code.
Make an edit of whatever choice, it could be just pressing a letter, and then revert the edit. This puts this position as the last edit position in memory. This "last edit"-trick is to get back to that position after the execution of the following selection.
Now press Ctrl+Shift+Pos1, or if you want to run all, take Ctrl+A instead.
Execute the selection with Alt+Shift+E.
Get back to the "last edit"-trick position Ctrl+Shift+Backspace to go to the last edit according to the question body at Keyboard shortcut for Jump to Previous View Location (Navigate back/forward) in IntelliJ IDEA
If you have selected some code, and you want to get to the bottom of the selection; or if you have just applied Alt+Shift+E and you realise that you have forgotten to edit something at PositonEND in order to use the trick above with Ctrl+Shift+Backspace, and you want to go back to PositionEND, you must simply copy / cut and paste the selected code, that will put your cursor to the end of the selection:
Ctrl+c (Ctrl+x would work as well)
Ctrl+v
By the way, if you are in somewhat else than in the editor and you want to turn back, do not click inside the editor, but just click on the needed ".py"-tab then so that your latest selection information does not get lost.
If
you can take a "risk" / try your luck /
coming back to a nearby place is enough,
you may use Ctrl+Alt+← (Left Arrow), though this is very often not helping you to get to that exact previous cursor place, being confusingly called: "Jump to Previous View Location". It does not always do what you would expect (I think).

How can I see function arguments in IPython Notebook Server 3?

I've recently switched to IPython Notebook 3 (3.1.0-cbccb68 to be exact), the Anaconda version. Previously when I typed a function and opened a parenthesis like this:
time.sleep()
and if the cursor was between the parentheses then I would get a contextual overlay menu that displayed the function arguments. Now I don't see it, and although I've searched, I can't find out how I can turn on this functionality.
In 1.0, the functionality was bound to ( and tab and shift-tab, in 2.0 tab was deprecated but still functional in some unambiguous cases completing or inspecting were competing in many cases. Recommendation was to always use shift-Tab. ( was also added as deprecated as confusing in Haskell-like syntax to also push people toward Shift-Tab as it works in more cases. in 3.0 the deprecated bindings have been remove in favor of the official, present for 18+ month now Shift-Tab.
So press Shift-Tab.
Try Shift-Tab-Tab a bigger documentation appears, than with Shift-Tab. It's the same but you can scroll down.
Shift-Tab-Tab-Tab and the tooltip will linger for 10 seconds while you type.
Shift-Tab-Tab-Tab-Tab and the docstring appears in the pager (small part at the bottom of the window) and stays there.
Adding screen shots(examples) and some more context for the answer of #Thomas G.
if its not working please make sure if you have executed code properly. In this case make sure import pandas as pd is ran properly before checking below shortcut.
Place the cursor in middle of parenthesis () before you use shortcut.
shift + tab
Display short document and few params
shift + tab + tab
Expands document with scroll bar
shift + tab + tab + tab
Provides document with a Tooltip: "will linger for 10secs while you type". which means it allows you write params and waits for 10secs.
shift + tab + tab + tab + tab
It opens a small window in bottom with option(top righ corner of small window) to open full documentation in new browser tab.
Shift-Tab works for me to view the dcoumentation

Ncurses, python, and OSX Lion

I'm new to nurses, and trying it out on my OSX Lion with some python code. I've ran across a weird bug, and I don't know what I'm doing wrong. I've Googled extensively, and can't find a similar issue, even in linux. I've selectively removed lines to see if one of them is an issue, also. When I run the code below, I get nothing. No menu, and my terminal is messed up, if I hit enter, you see what I get in the picture below. I have to type a reset to make it work well again. Can anyone give me suggestions, or point me in the direction where to look? I would really appreciate it. Thanks.
Script:
import curses
screen = curses.initscr() # Init curses
curses.noecho() # Suppress key output to screen
curses.curs_set(0) # remove cursor from screen
screen.keypad(1) # set mode when capturing keypresses
top_pos = 12
left_pos = 12
screen.addstr(top_pos, left_pos, "This is a String")
Result:
BTW, I'm using the default python and libs in Lion, no macports. I'd like to use the native libraries, if possible.
You have 2 problems.
After adding the string to the screen with addstr you don't tell it to refresh the screen. Add this after the call to addstr:
screen.refresh()
You need to call endwin() at the end of you program to reset the terminal. Add this to the end of your program:
curses.endwin()
That said, after making those 2 changes when you run your program it will appear to do nothing because after displaying the string on the screen curses exits and returns the screen to the state before you ran the program.
Add this before the call to endwin():
screen.getch()
Then it will wait for you press a key before exiting.

Categories

Resources