导出笔记本时隐藏stderr块

时间:2018-05-17 21:27:55

标签: jupyter-notebook customization jupyter nbconvert

我有一个自定义模板,可以从我的Jupyter笔记本导出到LaTeX。

((*- extends 'article.tplx' -*))

% Disable input cells
((* block input_group *))
((* endblock input_group *))

这样可以正常工作,但有时笔记本中的Python代码会产生警告和错误,这些警告和错误在笔记本中很有用,但在导出时却没有。例如,在一个笔记本中有这个块:

  {
   "cell_type": "code",
   "execution_count": 106,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\me\\Anaconda3\\lib\\site-packages\\ipykernel_launcher.py:3: FutureWarning: currently extract(expand=None) means expand=False (return Index/Series/DataFrame) but in a future version of pandas this will be changed to expand=True (return DataFrame)\n",
      "  This is separate from the ipykernel package so we can avoid doing imports until\n"
     ]
    }
   ],
   "source": [
    "# Some Python code using Pandas\n",
    "df = d.join(df.mycol.str.extract((?P<foo>\\d)')\n",
    ")"
   ]
  }

我想在导出时排除此类错误。我尝试将以下行添加到我的模板中:

% Hide Stderr output
((* block stream_stderr *))
((* endblock stream_stderr *))

但是,我仍然看到LaTeX输出中出现相应的错误。我做错了什么?

1 个答案:

答案 0 :(得分:1)

我发现自己也遇到了同样的问题,并提出了解决方案。将其放在您的模板文件中:

((* block stream *))
((*- if output.name == 'stderr' -*))
((*- else -*))
    \begin{Verbatim}[commandchars=\\\{\}]
((( output.text | escape_latex | ansi2latex )))
    \end{Verbatim}
((*- endif -*))
((* endblock stream *))