9

Is there a way to split the output console? I would like to display one section on top (the main program) and the bottom part will display a progress bar for example.

(excuse my horrible design skills)

enter image description here

Any ideas will be greatly appreciated :)

necross
  • 147
  • 2
  • 11
  • 4
    Take a look at [curses](https://docs.python.org/2/howto/curses.html) – Aran-Fey Aug 17 '16 at 15:06
  • Which console do you mean? Is it a Windows console? In which case you need to look at the Windows console APIs. – cdarke Aug 17 '16 at 15:07
  • it is a Linux Console – necross Aug 17 '16 at 15:12
  • In addition to `curses` (for splitting the screen and generating your own user interface on the terminal) you might take a look to [progressbar](http://progressbar-2.readthedocs.io/en/latest/index.html) which provides what it says. – albert Aug 17 '16 at 15:53
  • Why on earth would you run something like this in a LInux console!? Use a window management system and run it in a terminal! https://unix.stackexchange.com/questions/4126/what-is-the-exact-difference-between-a-terminal-a-shell-a-tty-and-a-con – William Pursell Nov 19 '22 at 12:27

2 Answers2

0

If there is one python app that outputs - using curses library as @Rawing suggested: https://docs.python.org/3.5/howto/curses.html . It's prebuilt and at hand.

If there are more apps that output data there are several ways to do so. First, you can use byobu or alike and have split terminal with outputs from different apps visible on the same screen. Second, you can have a broadcaster app that collects data from worker apps (or threads) and displays them later with curses (see above).

More, you can dump data to a file and then using Linux watch command show contents at regular intervals:

watch cat file

There are lots of other methods too.

Hibryda
  • 127
  • 1
  • 8
0

if you need two or multiple consoles for the output of your python script then you can do this if you are on windows. Use win32console module to open a second console for your thread or subprocess output.

Here is a sample code:

import win32console
import multiprocessing

def subprocess(queue):
    win32console.FreeConsole() #Frees subprocess from using main console
    win32console.AllocConsole() #Creates new console and all input and output of subprocess goes to this new console
    while True:
        print(queue.get())
        #prints any output produced by main script passed to subprocess using queue

if __name__ == "__main__": 
    queue = multiprocessing.Queue()
    multiprocessing.Process(target=subprocess, args=[queue]).start()
    while True:
        print("Hello World in main console")
        queue.put("Hello work in sub process console")
        #sends above string to subprocess and it prints it into its console

        #and whatever else you want to do in ur main process

You can also do this with threading. You have to use queue module if you want the queue functionality as threading module doesn't have queue

Here is the win32console module documentation