Forum Archive

Some Thoughts on the Console Module

steffen84

With the console module and its ability to print links, it is very easy to create interactive text based apps that work very well on a touch screen. The links always call the same script but with different parameters.

Here are some ideas that would further improve the usage of the console module:
1. Hide the console controls (input and the bar at the top). Or at least programatically hide the keyboard.
2. Define a custom popup menu for links (print_link() with an optional link list that are shown in a popup menu when the link is hold).
3. Get the device's orientation
4. Keep imported modules loaded
5. More input options for numeric or date/time values. Maybe even custom selections.
6. Images with links.
7. Control the scroll position - mainly scroll to top after printing.
8. Suppress update of the console output until all text has been printed. Especially useful in combination with 7.

It would be great if some of these ideas could be realized in the next version of pythonista.

Steffen

filippocld223

and console.copy() for copying text output

Sebastian

@filippocld Why don't you use pythonistas clipboard.set(string) to copy stuff?

ccc
http://stackoverflow.com/questions/4675728/redirect-stdout-to-a-file-in-python  # Will only work in single threaded apps!

from contextlib import contextmanager
from StringIO import StringIO

@contextmanager
def stdoutRedirected():
    """Save current stdout, redirect stdout to an in-memory
    file and then restore stdout when leaving the 'with'
    clause."""
    saveStdout = sys.stdout  # Save the current stdout.
    sys.stdout = StringIO() # Redirect stdout > in-memory file
    try:     yield None
    finally: sys.stdout = saveStdout # Restore original stdout

redirectedText = ''  # Holds the text after 'with' completes.
with stdoutRedirected():  # Redirect stdout to in-memory file.
    print('\n'.join('This text will not appear on the console but will instead be stored in an in-memory file.'.split()))
    # Final line of 'with' clause MUST save redirected text.
    redirectedText = sys.stdout.getvalue()  # Save hidden text

print('The "with stdoutRedirected():" clause has completed.')
print('=' * 54)
print(redirectedText) # Show that we still have hidden text
filippocld223

console.copy is more simple than this boring script....

filippocld223

I want to set the "out" variable to the entire output....all that's displayed on the output area....I don't want to copy it