简体   繁体   English

如何在运行 jupyter notebook 时从日志步骤中抑制 output?

[英]How do I suppress the output from log step while running jupyter notebook?

On this page, there is this log_step function which is being used to record what each step in a pandas pipeline is doing.这个页面上,有这个 log_step function 用于记录 pandas 管道中的每个步骤正在做什么。 The exact function is:确切的 function 是:

from functools import wraps
import datetime as dt

def log_step(func):
    @wraps(func)
    def wrapper(*args, **kwargs):
        tic = dt.datetime.now()
        result = func(*args, **kwargs)
        time_taken = str(dt.datetime.now() - tic)
        print(f"just ran step {func.__name__} shape={result.shape} took {time_taken}s")
        return result
    return wrapper

and it is used in the following fashion:它以下列方式使用:

import pandas as pd

df = pd.read_csv('https://calmcode.io/datasets/bigmac.csv')

@log_step
def start_pipeline(dataf):
    return dataf.copy()

@log_step
def set_dtypes(dataf):
    return (dataf
            .assign(date=lambda d: pd.to_datetime(d['date']))
            .sort_values(['currency_code', 'date']))

My question is: how do I keep the @log_step in front of my functions and be able to use them at will, while setting the results of @log_step by default, to not be outputed when I run my Jupyter notebook?我的问题是:如何将@log_step 保留在我的函数前面并能够随意使用它们,同时将@log_step 的结果默认设置为在我运行Jupyter notebook 时不输出? I suspect the answer comes down to something more general about using decorators but I don't really know what to look for.我怀疑答案归结为关于使用装饰器的更一般的东西,但我真的不知道要寻找什么。 Thanks!谢谢!

You can indeed remove the print statement or, if you want to not alter the decorating function, you can as well redirect the sys to avoid seeing the prints, as explained here .您确实可以删除print语句,或者,如果您不想更改装饰 function,您也可以重定向sys以避免看到打印,如此处所述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 How to suppress or prevent http header response returned from Box python API from printing in the cell output in Jupyter notebook - How to suppress or prevent http header response returned from Box python API from printing in the cell output in Jupyter notebook 对 Jupyter Notebook 单元的输出运行测试 - Running tests on the output of Jupyter Notebook cells 如何摆脱 jupyter notebook 中的关键错误? - how do i get rid of a key error in jupyter notebook? 如何在 jupyter 笔记本中显示压缩内容? - How do I display the zipped content in a jupyter notebook? 没有来自 Jupyter Notebook 的 OUTPUT。 未显示错误 - No OUTPUT from Jupyter Notebook. NO Error Shown 在 Jupyter Notebook 上从 ThinkStats 运行练习时出错 - Error running exercises from ThinkStats on Jupyter Notebook 如何在 jupyter 笔记本上仅显示最终的 output - how to show only the final output on jupyter notebook 如何将图从 Jupyter 笔记本保存到本地,而笔记本位于远程服务器上? - How to save figure from Jupyter notebook to local, while the notebook lives on a remote server? 我是否必须使用 jupyter notebook/lab/hub 在 Kubernetes 集群上运行 Dask? - Do I have to use jupyter notebook/lab/hub for running Dask on a Kubernetes cluster? 用python 3运行Jupyter笔记本 - Running Jupyter notebook with python 3
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM