简体   繁体   English

Python:print()始终写入.ipynb,使文件过大

[英]Python: print() always writes to .ipynb making file too large

To cut to the chase, I'd like to know how to make print statements display in ipython notebooks while simultaneously preventing those statements from saving to the .ipynb file. 为了顺理成章,我想知道如何在ipython笔记本中显示打印语句,同时防止这些语句保存到.ipynb文件中。 The purpose being to make a progress bar which doesn't make the file size ridiculously large. 目的是制作一个进度条,该进度条不会使文件的大小变得可笑。

The background to this is that I've been writing a bit of python code which makes a bunch of png files so that I can eventually compile them into a GIF. 这样做的背景是,我一直在编写一些python代码,这些代码制作了一堆png文件,以便最终将它们编译为GIF。 While I was doing this I thought I'd be clever and print the progress of the task as it went using print() from __future__ with carriage returns. 在执行此操作时,我认为我会很聪明,可以使用__future__ print()和回车符来print()任务的进度。 Unfortunately though I've had two problem with my code, both of which I imagine are related to my implementation of this progress message. 不幸的是,尽管我的代码有两个问题,但我认为这两个问题都与我对该进度消息的实现有关。

The first problem is with github's limit on file size: 第一个问题是github对文件大小的限制:

When I first tried to upload my file to github it prevented me from doing so because it exceeded their 100 MB limit. 当我第一次尝试将文件上传到github时,它阻止了我这样做,因为它超出了其100 MB的限制。 After investigating my .ipynb file I found that there was an obscene number of print statements which were being saved there. 在调查我的.ipynb文件之后,我发现那里保存着许多讨厌的打印语句。 Initially I'd thought that including '\\r' to do carriage returns would prevent this, but clearly that's not the case. 最初,我认为将'\\r'包含在内以防止回车,但是显然不是这种情况。

The second problem is probably related to this: 第二个问题可能与此有关:

Typically I don't have a problem creating the first few GIFs especially if I don't include that many frames, however beyond that my python notebook crashes. 通常,创建前几个GIF不会有问题,尤其是当我不包含那么多帧时,但是除此之外,我的python笔记本也会崩溃。 If this were a typical memory problem I'd imagine that it would just throw an error at me, but it doesn't, and instead promptly dies on me. 如果这是一个典型的内存问题,我想它会向我抛出一个错误,但是不会,而是立即对我死亡。

Here's a sample of the sort of stuff that's bloating the .ipynb file: 这是使.ipynb文件膨胀的一类示例:

{
   "output_type": "stream",
   "stream": "stdout",
   "text": [
    "\r",
    "frame 1 -- 0.480% complete -- U_avg = -7.200000\r",
    "frame 1 -- 0.500% complete -- U_avg = -7.200000\r",
    "frame 1 -- 0.520% complete -- U_avg = -7.200000\r",
    "frame 1 -- 0.540% complete -- U_avg = -7.200000\r",
    "frame 1 -- 0.560% complete -- U_avg = -7.200000\r",
    "frame 1 -- 0.580% complete -- U_avg = -7.200000\r",
    "frame 1 -- 0.600% complete -- U_avg = -7.200000\r",
    "frame 1 -- 0.620% complete -- U_avg = -7.200000\r",
    "frame 1 -- 0.640% complete -- U_avg = -7.200000\r",
    "frame 1 -- 0.660% complete -- U_avg = -7.200000\r",
    "frame 1 -- 0.680% complete -- U_avg = -7.200000\r",
    "frame 1 -- 0.700% complete -- U_avg = -7.200000\r",
    "frame 1 -- 0.720% complete -- U_avg = -7.200000\r", ......

I've looked into how other people have implemented progress bars, however they don't appear to do anything special which would actually prevent the problem I'm having. 我研究了其他人如何实现进度条,但是他们似乎并没有做任何特别的事情来实际上避免我遇到的问题。 If it comes down to it, I wouldn't mind importing something which would solve this problem in a black box, but at the same time if I run into this issue in a different context it would be useful to know how to reduce .ipynb file sizes by cutting out saved print statements. 如果涉及到这个问题,我不会介意导入一些可以解决此问题的方法,但是同时,如果我在其他情况下遇到此问题,了解如何减少.ipynb也会很有用。通过剪切保存的打印语句来确定文件大小。

Thanks! 谢谢!

I can think of two possible solutions: 我可以想到两种可能的解决方案:

  1. Add a conditional so that you only print when significant progress has been made? 添加条件,以便仅在取得重大进展时才打印? ie only print whole percentage points. 即只打印整个百分点。

  2. Use Javascript to display and update your progress bar. 使用Javascript显示和更新进度条。 The Javascript need to periodically call the ipython kernel to get progress update. Javascript需要定期调用ipython内核以获取进度更新。 Example: https://jakevdp.github.io/blog/2013/06/01/ipython-notebook-javascript-python-communication/ 示例: https//jakevdp.github.io/blog/2013/06/01/ipython-notebook-javascript-python-communication/

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM