Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dismiss "Black is not installed, parameters wont be formatted" message #661

Closed
edublancas opened this issue Mar 19, 2022 · 21 comments · Fixed by #831
Closed

dismiss "Black is not installed, parameters wont be formatted" message #661

edublancas opened this issue Mar 19, 2022 · 21 comments · Fixed by #831
Labels
good first issue Good for newcomers

Comments

@edublancas
Copy link
Contributor

papermill shows a logging message if black is not installed, which is causing confusion for ploomber users.

https://github.com/nteract/papermill/blob/aecd0ac16d34dd41a1a8e4ca3351844932af3d94/papermill/translators.py#L195

we should suppress the message

@rishav-karanjit
Copy link
Contributor

Hello @edublancas, you want to remove this message or replace it by something else?

@edublancas
Copy link
Contributor Author

we want to suppress it, note that this is coming from an external library (papermill), so I think we need to modify the command-line interface code to suppress warnings coming from papermill

to reproduce:

first, get an example

pip install ploomber
ploomber -n templates/ml-basic -o example
cd example
pip install -r requirements.txt

Then:

(ploomber)  Edu@MBP test/ml » ploomber nb -i                                     127 ↵
Loading pipeline...
Black is not installed, parameters wont be formatted
Black is not installed, parameters wont be formatted
Black is not installed, parameters wont be formatted
Black is not installed, parameters wont be formatted
Black is not installed, parameters wont be formatted
Black is not installed, parameters wont be formatted

Note: ensure black isn't installed in the environment

@rishav-karanjit
Copy link
Contributor

image

Looks good for me.

@edublancas
Copy link
Contributor Author

maybe you have black installed?

black --help

@rishav-karanjit
Copy link
Contributor

Will look into it @edublancas. Meanwhile, while looking into this issue I found another issue #664

@edublancas
Copy link
Contributor Author

edublancas commented Mar 19, 2022

sure, merged your PR, thanks!

as a tip, I think we want to use the logging module to suppress warnings coming from papermill.

when calling parametrize_notebook, we should temporarily suppress loggings coming from papermill, here's we were are calling parametrize_notebook (note that we are calling it in multiple places):

from papermill.parameterize import parameterize_notebook

@idomic
Copy link
Contributor

idomic commented May 23, 2022

@rishav-karanjit any updates here?

@rishav-karanjit
Copy link
Contributor

I don't think I would be working on this issue @idomic

@94rain
Copy link
Contributor

94rain commented May 24, 2022

For the reproduce example, I think pip install -r requirements.txt will actually install black for us. I need to run pip uninstall black to reproduce the warning.

Looking into how to suppress warnings from imported modules with logging.

@edublancas
Copy link
Contributor Author

hi @94rain, the message is coming from papermill, a library we use to execute notebooks. Ideally, we only want to suppress warnings when this message is displayed, as there might be other warnings that are important for the user. the first step is to figure out where exactly papermill is generating the warning. it should be in one of these two places:

when we prepare the notebook:

https://github.com/ploomber/ploomber/blob/master/src/ploomber/sources/notebooksource.py

or when we execute it:

https://github.com/ploomber/ploomber/blob/master/src/ploomber/tasks/notebook.py

@94rain
Copy link
Contributor

94rain commented May 25, 2022

I am able to suppress all messages output by the codify function (which outputs the Black is not installed message) using the following code snippet in notebooksource.py.

+   logging.basicConfig(level=logging.ERROR)
     from papermill.parameterize import parameterize_notebook
+    class IgnoreBlackWarning(logging.Filter):
+        def filter(self, record):
+            return not record.funcName == 'codify'
+    logging.getLogger("papermill").addHandler(IgnoreBlackWarning)

I am not sure if there are simpler ways to implement this. I tried to do things likelogging.getLogger("papermill. codify").addHandler(logging.NullHandler()) (or replace papermill with other module names) but does not seem to work.

I'm not sure if adding logging.basicConfig(level=logging.ERROR) there will have some unexpected consequences, and I am still looking into it. (I think we need to pass in the arguments of --log from cli to here as well)

@idomic
Copy link
Contributor

idomic commented May 25, 2022

@94rain does it suppress all warnings or just black? I think the better option instead of instantiating a logger is to catch the specific black warning.
Changing the logger level will definitely remove all warnings so we shouldn't do something like that.

Did you try something along the lines of the functions Eduardo sent?

with warnings.catch_warnings():
    warnings.simplefilter('ignore', FutureWarning)
    from papermill.parameterize import parameterize_notebook

Then in it you can match the warning text and remove it only if there's a match.

@94rain
Copy link
Contributor

94rain commented May 25, 2022

At this time the snippet above will suppress all warnings. I mean we can pass in the logger level from cli?

logging.basicConfig(level=args.log.upper())

Yeah, I tried things in that block like the following but they do not seem to work

    warnings.filterwarnings(action='ignore', message=r'.*Black.*')
    warnings.filterwarnings(action='ignore', message='Black is not installed, parameters wont be formatted.')
    warnings.filterwarnings(action='ignore', module=r'.*papermill.*translators.*')

So we have to use the logging module (The warning is generated by logging). I am still finding out if there are easy ways to do text matching.

Another possible approach is to suppress all warnings from the logger of papermill before calling parameterize_notebook and restore it after that

model['content'] = parameterize_notebook(nb,

@idomic
Copy link
Contributor

idomic commented May 26, 2022

You're right, for some reason even when putting in the main class:

import warnings
warnings.filterwarnings("ignore")

Which should ignore all warnings, this is still being printed, not sure exactly why.
We can use the logging module to fix this specific one and later explore what's going on.
You can set it as part of the initial code block where we import the papermill function
@edublancas thoughts?

@edublancas
Copy link
Contributor Author

@94rain is right: papermill is generating the message using logging, so using warnings.filterwarnings has no effect. So the question becomes: how do we use logging to suppress the message.

@94rain
Copy link
Contributor

94rain commented Jun 1, 2022

I am able to suppress all messages output by the codify function (which outputs the Black is not installed message) using the following code snippet in notebooksource.py.

+   logging.basicConfig(level=logging.ERROR)
     from papermill.parameterize import parameterize_notebook
+    class IgnoreBlackWarning(logging.Filter):
+        def filter(self, record):
+            return not record.funcName == 'codify'
+    logging.getLogger("papermill").addHandler(IgnoreBlackWarning)

I am not sure if there are simpler ways to implement this. I tried to do things likelogging.getLogger("papermill. codify").addHandler(logging.NullHandler()) (or replace papermill with other module names) but does not seem to work.

I'm not sure if adding logging.basicConfig(level=logging.ERROR) there will have some unexpected consequences, and I am still looking into it. (I think we need to pass in the arguments of --log from cli to here as well)

After some retries, I just realized what I did wrong here. I need to use addHandler(IgnoreBlackWarning()) (with constructor) instead of addHandler(IgnoreBlackWarning), so it is no longer needed to set logger level.

The following code snippet is able to remove only the messages that contain Black is not installed.

with warnings.catch_warnings():
    warnings.simplefilter('ignore', FutureWarning)
    class IgnoreBlackWarning(logging.Filter):
        def filter(self, record):
            return 'Black is not installed' not in record.msg
    logging.getLogger("papermill.translators").addFilter(IgnoreBlackWarning())

I will create a PR tomorrow. Do we need test cases for this?

@edublancas
Copy link
Contributor Author

great, thanks for figuring this out. no need to add test cases

question, do we need this?

with warnings.catch_warnings():

I thought we didn't need to use the warnings module, maybe this alone will work?

    class IgnoreBlackWarning(logging.Filter):
        def filter(self, record):
            return 'Black is not installed' not in record.msg
    logging.getLogger("papermill.translators").addFilter(IgnoreBlackWarning())

@94rain
Copy link
Contributor

94rain commented Jun 2, 2022

That alone will work for suppressing the black warning.

I found there is a comment above papermill is importing a deprecated module from pyarrow. I don't know much about the context of this. Looks like it is used to suppress a deprecation warning. Is it still showing up? How can this warning be reproduced?

@edublancas
Copy link
Contributor Author

ok, so let's just keep the code to remove the black warning and remove the catch_warnings

about pyarrow: they fixed that in the latest papermill release, so no need to worry about it.

@94rain
Copy link
Contributor

94rain commented Jun 2, 2022

Sounds good! Will remove with warnings.catch_warnings(): and create a PR soon.

@idomic
Copy link
Contributor

idomic commented Oct 11, 2022 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Good for newcomers
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants