我正在玩我的iPython笔记本格式化,以便将它们变成日志。
display(HTML())
构造的使用使得一切都很好并且相对容易组织。
我想在其他媒体中使用某些单元格的输出,例如演示文稿。我现在这样做的方法是截取该区域的截图,但随后所有内容都变为像素,并且无法进行细化。
有没有办法以某种有用的格式呈现单个单元格的输出?
答案 0 :(得分:1)
您可以尝试%%capture Jupyter notebook
命令。
答案 1 :(得分:0)
在Jupyter as of now中无法导出单个单元格输出,但是您可以做的是将整个笔记本转换为更有用的格式,然后仅修剪所需的部分。这不是最佳选择,但至少在输出质量方面,它比您当前的解决方法要好。
您可以通过不同的方式执行此操作:
如果要以编程方式进行操作,可以在命令行中使用 nbconvert ,如下所示:
<?php
require('fpdf.php');
class PDF_MySQL_Table extends FPDF
{
protected $ProcessingTable=false;
protected $aCols=array();
protected $TableX;
protected $HeaderColor;
protected $RowColors;
protected $ColorIndex;
function Header()
{
// Print the table header if necessary
if($this->ProcessingTable)
$this->TableHeader();
}
function TableHeader()
{
$this->SetFont('Arial','B',12);
$this->SetX($this->TableX);
$fill=!empty($this->HeaderColor);
if($fill)
$this->SetFillColor($this->HeaderColor[0],$this->HeaderColor[1],$thisHeaderColor[2]);
foreach($this->aCols as $col)
$this->Cell($col['w'],6,$col['c'],1,0,'C',$fill);
$this->Ln();
}
function Row($data)
{
$this->SetX($this->TableX);
$ci=$this->ColorIndex;
$fill=!empty($this->RowColors[$ci]);
if($fill)
$this->SetFillColor($this->RowColors[$ci][0],$this->RowColors[$ci][1],$this->RowColors[$ci][2]);
foreach($this->aCols as $col)
$this->Cell($col['w'],5,$data[$col['f']],1,0,$col['a'],$fill);
$this->Ln();
$this->ColorIndex=1-$ci;
}
function CalcWidths($width, $align)
{
// Compute the widths of the columns
$TableWidth=0;
foreach($this->aCols as $i=>$col)
{
$w=$col['w'];
if($w==-1)
$w=$width/count($this->aCols);
elseif(substr($w,-1)=='%')
$w=$w/100*$width;
$this->aCols[$i]['w']=$w;
$TableWidth+=$w;
}
// Compute the abscissa of the table
if($align=='C')
$this->TableX=max(($this->w-$TableWidth)/2,0);
elseif($align=='R')
$this->TableX=max($this->w-$this->rMargin-$TableWidth,0);
else
$this->TableX=$this->lMargin;
}
function AddCol($field=-1, $width=-1, $caption='', $align='L')
{
// Add a column to the table
if($field==-1)
$field=count($this->aCols);
$this->aCols[]=array('f'=>$field,'c'=>$caption,'w'=>$width,'a'=>$align);
}
function Table($con, $query, $prop=array())
{
// Execute query
$res=mysqli_query($con,$query) or die('Error: '.mysqli_error($con)." <br>Query: $query");
// Add all columns if none was specified
if(count($this->aCols)==0)
{
$nb=mysqli_num_fields($res);
for($i=0;$i<$nb;$i++)
$this->AddCol();
}
// Retrieve column names when not specified
foreach($this->aCols as $i=>$col)
{
if($col['c']=='')
{
if(is_string($col['f']))
$this->aCols[$i]['c']=ucfirst($col['f']);
else
$this->aCols[$i]['c']=ucfirst(mysqli_fetch_field_direct($res,$col['f'])->name);
}
}
// Handle properties
if(!isset($prop['width']))
$prop['width']=0;
if($prop['width']==0)
$prop['width']=$this->w-$this->lMargin-$this->rMargin;
if(!isset($prop['align']))
$prop['align']='C';
if(!isset($prop['padding']))
$prop['padding']=$this->cMargin;
$cMargin=$this->cMargin;
$this->cMargin=$prop['padding'];
if(!isset($prop['HeaderColor']))
$prop['HeaderColor']=array();
$this->HeaderColor=$prop['HeaderColor'];
if(!isset($prop['color1']))
$prop['color1']=array();
if(!isset($prop['color2']))
$prop['color2']=array();
$this->RowColors=array($prop['color1'],$prop['color2']);
// Compute column widths
$this->CalcWidths($prop['width'],$prop['align']);
// Print header
$this->TableHeader();
// Print rows
$this->SetFont('Arial','',11);
$this->ColorIndex=0;
$this->ProcessingTable=true;
while($row=mysqli_fetch_array($res))
$this->Row($row);
$this->ProcessingTable=false;
$this->cMargin=$cMargin;
$this->aCols=array();
}
}
?>
<?php
session_start();
class PDF extends PDF_MySQL_Table
{
function Header()
{
// Title
$this->SetFont('Arial','',18);
$this->Cell(0,6,'Server/URL Details',0,1,'C');
$this->Ln(10);
// Ensure table header is printed
parent::Header();
}
}
$pdf = new PDF();
$pdf->AddPage();
$query = "SELECT DISTINCT Server_Name,Server_Status,Server_Type FROM server where ENV_NAME='{$_SESSION['envt_name']}' && ENV_TYPE = '{$_SESSION['envt_type']}'";
$pdf->Table($con,$query);
$pdf->AddPage();
$querytwo = "SELECT DISTINCT URL_Name,URL_Status,URL_Type FROM url where ENV_NAME='{$_SESSION['envt_name']}' && ENV_TYPE = '{$_SESSION['envt_type']}'";
$pdf->Table($con,$querytwo);
$pdf->Output();
?>
您也可以在笔记本内部执行此操作,在单元格中执行以下代码(在此示例中为HTML):
nbconvert --to (your preferred output format) yourNotebook.ipynb
但是,大多数库允许您将程序的结果导出到任何所需的输出(熊猫,matplotlib,altair ...),因此您可能应该尝试使用它们。