redirect sys.stdout to specific Jupyter Notebook cell

The documentation for ipywidgets.Output has a section about interacting with output widgets from background threads. Using the Output.append_stdout method there is no need for locking. The final cell in this answer can then be replaced with: def t1_main(): for i in range(10): output1.append_stdout(f’thread1 {i}\n’) time.sleep(0.5) def t2_main(): for i in range(10): output2.append_stdout(f’thread2 {i}\n’) time.sleep(0.5) output1.clear_output() … Read more

Redirect standard input dynamically in a bash script

First of all stdin is file descriptor 0 (zero) rather than 1 (which is stdout). You can duplicate file descriptors or use filenames conditionally like this: [[ some_condition ]] && exec 3<“$filename” || exec 3<&0 some_long_command_line <&3 Note that the command shown will execute the second exec if either the condition is false or the … Read more

What method should I use to write error messages to ‘stderr’ using ‘printf’ in a bash script?

First, yes, 1>&2 is the right thing to do. Second, the reason your 1>&2 2>errors.txt example doesn’t work is because of the details of exactly what redirection does. 1>&2 means “make filehandle 1 point to wherever filehandle 2 does currently” — i.e. stuff that would have been written to stdout now goes to stderr. 2>errors.txt … Read more