PlagTag
PlagTag

Reputation: 6429

Click not working in jupyter

just found the following . running via notebook

import 
.echo("test")

Output:

/home/user/anaconda3/envs/p36/lib/python3.6/site-packages//utils.py in echo(message, file, nl, err, color)
    257 
    258     if message:
--> 259         file.write(message)
    260     file.flush()
    261 

UnsupportedOperation: not writable

Has someone seen this before and knows how to work around? I have to use a lib via that uses . so not is not possible.

Update: This commit to a jupyter branch of click solves the issue: https://github.com/elgalu/click/commit/1cb7aaba8c9dd6ec760d3e7e414d0b4e5f788543#diff-d17772ee4f65879b69a53dbc4b3d42bd

Upvotes: 2

Views: 1217

Answers (2)

dmmfll
dmmfll

Reputation: 2826

In my case using Python 3 I wanted to preserve the click styling on my message both in the Jupyter notebook and when the code was run in the terminal. I handled it this way:

from io import UnsupportedOperation
import click

item = 'Your Name'
message = click.style('"{}"'.format(item), fg='red', bold=True)
try:
    click.echo(message)
except UnsupportedOperation as err:
    print('Error: "{}"'.format(err))
    print(message)

The color is preserved in the notebook:

click styling is preserved

Upvotes: 1

zwer
zwer

Reputation: 25769

I think that Jupyter hijacks and locks the STDOUT/STDERR (at least the one click is trying to use) and if you don't provide a stream to click.echo() it will attempt writing to the STDOUT/STDERR, hence the error.

You can work around it by passing an output stream like STDOUT yourself:

import click
import sys

click.echo("test", sys.stdout)
# test

Upvotes: 4

Related Questions