problems with the curses.newwin() command - python

when using the curses.newwin() command
e.g.
curses.newwin(10, 10, 0, 0)
if i try to edit the integers to create a larger window the program terminates when I try to run it.

As the comments have mentioned, you can't make a larger window than your parent terminal. If you're looking for a way to resize the terminal itself, consider something like this:
os.system("mode con cols=80 lines=60")
os.environ['COLS'] = "80"
os.environ['LINES'] = "60"
This will change the console size. You can change the size to whatever you like, just change cols and lines as needed. The first line sets the upper limit on the bottom two--you'll throw an error if one of the lower numbers is larger.

Related

Jupyter notebook: how to leave one cell out while 'run all'

I'm writing python using jupyter notebook and I have two cells that can influence each other.
I'm wondering is it possible to leave some certain cells out after I click Restart & Run All so that I can test the two cells independently?
One option based on Davide Fiocco's answer of this post and that I just tested is to include %%script magic command on each cell you don't want to execute.
For example
%%script false --no-raise-error
for i in range(100000000000000):
print(i)
If you put those two cells at the end of the page, you can run all cells above a certain cell with a single click.
That or you can put a triple-quote at the beginning and end of the two cells, then un-quote the cells to test them.
One option is to create a parameter and run the cells accordingly
x = 1
# cell 1
if x == 1:
// run this cell
# cell 2
if x != 1:
// run the other cell
In this example, you will skip cell 2.
I recently discovered an easy way to do this.
You may have noticed that cells can be set as type Code or Markdown - this lets you prepare the notebook with headers and explanatory text (in Markdown), but also sections of executable code (the default). This can be set from a drop-down already on the screen if using via Jupyter Lab. In Jupyter Notebook I think it's under the Cells menu.
You can also use keyboard shortcuts (first hit Escape if needed to get out of text-entry mode: Y for Code, Mfor Markdown, or Rfor Raw.
Wait, what's that about Raw? It appears to just take away the code highlighting and make the cell not executable! So Esc+R to make it Raw, then execute like you wanted to, then Esc+Y if you want to re-enable that block.
Alternative: If you want a quicker way to comment out all the lines but leave it as a Code block, make sure you are in edit mode (click on the cell contents), do Ctrl+A (for select-all), and then Ctrl+/ (for "comment this line"). I tested with python and it inserts # at the beginning of each selected line.

How to have a horizontal scroll bar when a column in the output is really long when using Jupyter and Python

I am trying to use Jupyter + Python. Here is an example of the output
You can see the because the column 'correspondencedata' is too long, so it can not be shown fully in the output.
Can I change this so that a horizontal scroll bar will occur when a column has too long content?
You want to use pd.set_option('max_colwidth', nbr_pixel) before.
If you use a number big enough it will always show the entire content of your cells.
Like, pd.set_option('max_colwidth', 4000)
For more informations:
## To see the actual settings :
pd.get_option("display.max_colwidth")
## To reset with default value
pd.reset_option("max_colwidth")
Documentation

Width as formular but if value smaller than x be a fix value inline?

I draw texts with wxPython on my DC. The position of the text is depending on the width of my frame, in this case the variable "w".
It is calculated in my code like this:
dc.DrawText("Overview", w/2-wt/2, 10)
But now, if the forumlar "w/2-wt/2" is smaller than a certain value I want to set it to a fix value.
I know how to do it with an if than else, but my personal favorit would be to do it inline, to keep my code short an simple.
Is there a way to do this?

Formatting Strings in Python

I'm printing a title in python and I want it on the center of the screen.
I know I can do it by using
"{:^50}".format("Title")
But the thing with this command is it only utilizes the width I give in (in this case, 50). But it isn't perfect and is sometimes way off. Even if I approximate the width by observing/guessing, it would go out of format if I re-size the terminal. I always want to align it on the middle of the screen, even when the terminal is re-sized(say, in fullscreen mode). Any ways I can achieve this?
EDIT:
I have did this:
Well, I figured out the way to find the window size,
import os
columns = os.popen('stty size', 'r').read().split()[0]
"{:^"+columns+"}".format("Title")
but the last line shows error. I finally have the window size, but I cannot format it correctly. Any help is appreciated!
As zondo pointed out, the title won't reposition when the window is resized.
The correct way to do this: "{:^"+columns+"}".format("Title") is like so:
"{:^{}}".format("Title", width)
#^---------------^ first argument goes with first brace
# ^--------------------^ second argument goes with second brace and sets the width

Output colored, right aligned text into the terminal

I want to output colored text completly aligned to the right in the terminal (like in this screenshot of pacman (packet manager of the arch linux distribution)(not colored))
Currently I'm using format:
import shutil
left = "foo"
right = "bar"
width = shutil.get_terminal_size().columns
template = "{left:30}{right:{width}}".format(left=left, right=right, width=width-30)
click.echo(template)
# click.echo works just like print with some additional features
This works great until I add colors via ANSI escape codes:
left = click.style("foo", fg="red")
right = click.style("bar", fg="green")
# click.style just adds ANSI codes for colors and bold etc.
Which looks like this:
I.e. the right side is not completly right aligned. Which is "right", because right is actually \\x1b[32mbar\\x1b[0m which of course has a higher lenght than bar and thus needs less spaces to be right aligned. Until the terminal gets the text and only displays bar (with color).
Am I missing anything in the python std lib or click? Or is there a simple library that deals with terminal colors and alignment that could help me? Or is there a simple solution to this problem?
click's documentation does not mention alignment (which is why you are using python's built-in string class). You could stay within the current set of interfaces by telling your script to remember the lengths of the strings before calling click.style, and adding the difference to the width used for the format call. (This would not work as well if you were centering text).
There are perhaps other libraries, but you could use the curses interface with the filter function to draw single-line displays.
First, I want to figure out that click provide a way to get the terminal size: click.get_terminal_size, the documentation is here
>>> import click
>>> click.get_terminal_size()
>>> (66, 24) # (width, height)
And my solution, should works even you resize the terminal:
width = click.get_terminal_size()[0]
left = click.style("foo", fg="red")
right = click.style("bar", fg="green")
print "{0:}{1:>{2}}".format(left, right, width+6)
Since right actually is \x1b[32mbar\x1b[0m, we increase the width by hand to avoid the problem.
Edit: PyFormat is useful for me when do string format in python. It helps me understand string formating. Hope it will help you.

Categories