blob: 80ae9938a4c3036ce68b8be4e8ac50ef59f55d1e [file]
<!DOCTYPE html>
<html lang="en" data-content_root="../../" data-theme="auto">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="viewport" content="width=device-width, initial-scale=1" />
<title>DataFrame Rendering &#8212; Apache DataFusion in Python documentation</title>
<script data-cfasync="false">
document.documentElement.dataset.mode = localStorage.getItem("mode") || "auto";
document.documentElement.dataset.theme = localStorage.getItem("theme") || "auto";
</script>
<!--
this give us a css class that will be invisible only if js is disabled
-->
<noscript>
<style>
.pst-js-only { display: none !important; }
</style>
</noscript>
<!-- Loaded before other Sphinx assets -->
<link href="../../_static/styles/theme.css?digest=8878045cc6db502f8baf" rel="stylesheet" />
<link href="../../_static/styles/pydata-sphinx-theme.css?digest=8878045cc6db502f8baf" rel="stylesheet" />
<link rel="stylesheet" type="text/css" href="../../_static/pygments.css?v=8f2a1f02" />
<link rel="stylesheet" type="text/css" href="../../_static/mystnb.11b39860a7a0cbfd473a3ad8a317855267ff0bd372690045ca344a6b62be495e.css" />
<link rel="stylesheet" type="text/css" href="../../_static/graphviz.css?v=4ae1632d" />
<link rel="stylesheet" type="text/css" href="../../_static/theme_overrides.css?v=4af573bc" />
<!-- So that users can add custom icons -->
<script src="../../_static/scripts/fontawesome.js?digest=8878045cc6db502f8baf"></script>
<!-- Pre-loaded scripts that we'll load fully later -->
<link rel="preload" as="script" href="../../_static/scripts/bootstrap.js?digest=8878045cc6db502f8baf" />
<link rel="preload" as="script" href="../../_static/scripts/pydata-sphinx-theme.js?digest=8878045cc6db502f8baf" />
<script src="../../_static/documentation_options.js?v=5929fcd5"></script>
<script src="../../_static/doctools.js?v=9bcbadda"></script>
<script src="../../_static/sphinx_highlight.js?v=dc90522c"></script>
<script>DOCUMENTATION_OPTIONS.pagename = 'user-guide/dataframe/rendering';</script>
<script src="../../_static/toc-toggle.js?v=36375f01"></script>
<link rel="icon" href="../../_static/favicon.svg"/>
<link rel="index" title="Index" href="../../genindex.html" />
<link rel="search" title="Search" href="../../search.html" />
<link rel="next" title="Execution Metrics" href="execution-metrics.html" />
<link rel="prev" title="DataFrames" href="index.html" />
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<meta name="docsearch:language" content="en"/>
<meta name="docsearch:version" content="" />
</head>
<body data-bs-spy="scroll" data-bs-target=".bd-toc-nav" data-offset="180" data-bs-root-margin="0px 0px -60%" data-default-mode="auto">
<div id="pst-skip-link" class="skip-link d-print-none"><a href="#main-content">Skip to main content</a></div>
<div id="pst-scroll-pixel-helper"></div>
<button type="button" class="btn rounded-pill" id="pst-back-to-top">
<i class="fa-solid fa-arrow-up"></i>Back to top</button>
<dialog id="pst-search-dialog">
<form class="bd-search d-flex align-items-center"
action="../../search.html"
method="get">
<i class="fa-solid fa-magnifying-glass"></i>
<input type="search"
class="form-control"
name="q"
placeholder="Search the docs ..."
aria-label="Search the docs ..."
autocomplete="off"
autocorrect="off"
autocapitalize="off"
spellcheck="false"/>
<span class="search-button__kbd-shortcut"><kbd class="kbd-shortcut__modifier">Ctrl</kbd>+<kbd>K</kbd></span>
</form>
</dialog>
<div class="pst-async-banner-revealer d-none">
<aside id="bd-header-version-warning" class="d-none d-print-none" aria-label="Version warning"></aside>
</div>
<header class="bd-header navbar navbar-expand-lg bd-navbar d-print-none">
<div class="bd-header__inner bd-page-width">
<button class="pst-navbar-icon sidebar-toggle primary-toggle" aria-label="Site navigation">
<span class="fa-solid fa-bars"></span>
</button>
<div class="col-lg-3 navbar-header-items__start">
<div class="navbar-item">
<a class="navbar-brand logo" href="../../index.html">
<img src="../../_static/original.svg" class="logo__image only-light" alt="Apache DataFusion in Python"/>
<img src="../../_static/original_dark.svg" class="logo__image only-dark pst-js-only" alt="Apache DataFusion in Python"/>
</a></div>
</div>
<div class="col-lg-9 navbar-header-items">
<div class="me-auto navbar-header-items__center">
<div class="navbar-item">
<nav>
<ul class="bd-navbar-elements navbar-nav">
<li class="nav-item current active">
<a class="nav-link nav-internal" href="../index.html">
User Guide
</a>
</li>
<li class="nav-item ">
<a class="nav-link nav-internal" href="../../contributor-guide/index.html">
Contributor Guide
</a>
</li>
<li class="nav-item ">
<a class="nav-link nav-internal" href="../../autoapi/index.html">
API Reference
</a>
</li>
<li class="nav-item ">
<a class="nav-link nav-internal" href="../../links.html">
Links
</a>
</li>
</ul>
</nav></div>
</div>
<div class="navbar-header-items__end">
<div class="navbar-item navbar-persistent--container">
<button class="btn search-button-field search-button__button pst-js-only" title="Search" aria-label="Search" data-bs-placement="bottom" data-bs-toggle="tooltip">
<i class="fa-solid fa-magnifying-glass"></i>
<span class="search-button__default-text">Search</span>
<span class="search-button__kbd-shortcut"><kbd class="kbd-shortcut__modifier">Ctrl</kbd>+<kbd class="kbd-shortcut__modifier">K</kbd></span>
</button>
</div>
<div class="navbar-item"><ul class="navbar-icon-links"
aria-label="Icon Links">
<li class="nav-item">
<a href="https://github.com/apache/datafusion-python" title="GitHub" class="nav-link pst-navbar-icon" rel="noopener" target="_blank" data-bs-toggle="tooltip" data-bs-placement="bottom"><i class="fa-brands fa-github fa-lg" aria-hidden="true"></i>
<span class="sr-only">GitHub</span></a>
</li>
<li class="nav-item">
<a href="https://docs.rs/datafusion/latest/datafusion/" title="Rust API docs (docs.rs)" class="nav-link pst-navbar-icon" rel="noopener" target="_blank" data-bs-toggle="tooltip" data-bs-placement="bottom"><i class="fa-brands fa-rust fa-lg" aria-hidden="true"></i>
<span class="sr-only">Rust API docs (docs.rs)</span></a>
</li>
</ul></div>
<div class="navbar-item">
<button class="btn btn-sm nav-link pst-navbar-icon theme-switch-button pst-js-only" aria-label="Color mode" data-bs-title="Color mode" data-bs-placement="bottom" data-bs-toggle="tooltip">
<i class="theme-switch fa-solid fa-sun fa-lg" data-mode="light" title="Light"></i>
<i class="theme-switch fa-solid fa-moon fa-lg" data-mode="dark" title="Dark"></i>
<i class="theme-switch fa-solid fa-circle-half-stroke fa-lg" data-mode="auto" title="System Settings"></i>
</button></div>
</div>
</div>
<div class="navbar-persistent--mobile">
<button class="btn search-button-field search-button__button pst-js-only" title="Search" aria-label="Search" data-bs-placement="bottom" data-bs-toggle="tooltip">
<i class="fa-solid fa-magnifying-glass"></i>
<span class="search-button__default-text">Search</span>
<span class="search-button__kbd-shortcut"><kbd class="kbd-shortcut__modifier">Ctrl</kbd>+<kbd class="kbd-shortcut__modifier">K</kbd></span>
</button>
</div>
<button class="pst-navbar-icon sidebar-toggle secondary-toggle" aria-label="On this page">
<span class="fa-solid fa-outdent"></span>
</button>
</div>
</header>
<div class="bd-container">
<div class="bd-container__inner bd-page-width">
<dialog id="pst-primary-sidebar-modal"></dialog>
<div id="pst-primary-sidebar" class="bd-sidebar-primary bd-sidebar">
<div class="sidebar-header-items sidebar-primary__section">
<div class="sidebar-header-items__center">
<div class="navbar-item">
<nav>
<ul class="bd-navbar-elements navbar-nav">
<li class="nav-item current active">
<a class="nav-link nav-internal" href="../index.html">
User Guide
</a>
</li>
<li class="nav-item ">
<a class="nav-link nav-internal" href="../../contributor-guide/index.html">
Contributor Guide
</a>
</li>
<li class="nav-item ">
<a class="nav-link nav-internal" href="../../autoapi/index.html">
API Reference
</a>
</li>
<li class="nav-item ">
<a class="nav-link nav-internal" href="../../links.html">
Links
</a>
</li>
</ul>
</nav></div>
</div>
<div class="sidebar-header-items__end">
<div class="navbar-item"><ul class="navbar-icon-links"
aria-label="Icon Links">
<li class="nav-item">
<a href="https://github.com/apache/datafusion-python" title="GitHub" class="nav-link pst-navbar-icon" rel="noopener" target="_blank" data-bs-toggle="tooltip" data-bs-placement="bottom"><i class="fa-brands fa-github fa-lg" aria-hidden="true"></i>
<span class="sr-only">GitHub</span></a>
</li>
<li class="nav-item">
<a href="https://docs.rs/datafusion/latest/datafusion/" title="Rust API docs (docs.rs)" class="nav-link pst-navbar-icon" rel="noopener" target="_blank" data-bs-toggle="tooltip" data-bs-placement="bottom"><i class="fa-brands fa-rust fa-lg" aria-hidden="true"></i>
<span class="sr-only">Rust API docs (docs.rs)</span></a>
</li>
</ul></div>
<div class="navbar-item">
<button class="btn btn-sm nav-link pst-navbar-icon theme-switch-button pst-js-only" aria-label="Color mode" data-bs-title="Color mode" data-bs-placement="bottom" data-bs-toggle="tooltip">
<i class="theme-switch fa-solid fa-sun fa-lg" data-mode="light" title="Light"></i>
<i class="theme-switch fa-solid fa-moon fa-lg" data-mode="dark" title="Dark"></i>
<i class="theme-switch fa-solid fa-circle-half-stroke fa-lg" data-mode="auto" title="System Settings"></i>
</button></div>
</div>
</div>
<div class="sidebar-primary-items__start sidebar-primary__section">
<div class="sidebar-primary-item">
<nav class="bd-docs-nav bd-links" aria-label="Section Navigation">
<p class="bd-links__title" role="heading" aria-level="1">Section Navigation</p>
<div class="bd-toc-item navbar-nav">
<ul class="current nav bd-sidenav">
<li class="toctree-l1 current active has-children"><a class="reference internal" href="../index.html">User Guide</a><details open="open"><summary><span class="toctree-toggle" role="presentation"><i class="fa-solid fa-chevron-down"></i></span></summary><ul class="current">
<li class="toctree-l2"><a class="reference internal" href="../introduction.html">Introduction</a></li>
<li class="toctree-l2"><a class="reference internal" href="../basics.html">Concepts</a></li>
<li class="toctree-l2"><a class="reference internal" href="../data-sources.html">Data Sources</a></li>
<li class="toctree-l2 current active has-children"><a class="reference internal" href="index.html">DataFrames</a><details open="open"><summary><span class="toctree-toggle" role="presentation"><i class="fa-solid fa-chevron-down"></i></span></summary><ul class="current">
<li class="toctree-l3 current active"><a class="current reference internal" href="#">DataFrame Rendering</a></li>
<li class="toctree-l3"><a class="reference internal" href="execution-metrics.html">Execution Metrics</a></li>
</ul>
</details></li>
<li class="toctree-l2 has-children"><a class="reference internal" href="../common-operations/index.html">Common Operations</a><details><summary><span class="toctree-toggle" role="presentation"><i class="fa-solid fa-chevron-down"></i></span></summary><ul>
<li class="toctree-l3"><a class="reference internal" href="../common-operations/views.html">Registering Views</a></li>
<li class="toctree-l3"><a class="reference internal" href="../common-operations/basic-info.html">Basic Operations</a></li>
<li class="toctree-l3"><a class="reference internal" href="../common-operations/select-and-filter.html">Column Selections</a></li>
<li class="toctree-l3"><a class="reference internal" href="../common-operations/expressions.html">Expressions</a></li>
<li class="toctree-l3"><a class="reference internal" href="../common-operations/joins.html">Joins</a></li>
<li class="toctree-l3"><a class="reference internal" href="../common-operations/functions.html">Functions</a></li>
<li class="toctree-l3"><a class="reference internal" href="../common-operations/spark-functions.html">Spark-Compatible Functions</a></li>
<li class="toctree-l3"><a class="reference internal" href="../common-operations/aggregations.html">Aggregation</a></li>
<li class="toctree-l3"><a class="reference internal" href="../common-operations/windows.html">Window Functions</a></li>
<li class="toctree-l3"><a class="reference internal" href="../common-operations/udf-and-udfa.html">User-Defined Functions</a></li>
</ul>
</details></li>
<li class="toctree-l2 has-children"><a class="reference internal" href="../io/index.html">IO</a><details><summary><span class="toctree-toggle" role="presentation"><i class="fa-solid fa-chevron-down"></i></span></summary><ul>
<li class="toctree-l3"><a class="reference internal" href="../io/arrow.html">Arrow</a></li>
<li class="toctree-l3"><a class="reference internal" href="../io/avro.html">Avro</a></li>
<li class="toctree-l3"><a class="reference internal" href="../io/csv.html">CSV</a></li>
<li class="toctree-l3"><a class="reference internal" href="../io/json.html">JSON</a></li>
<li class="toctree-l3"><a class="reference internal" href="../io/parquet.html">Parquet</a></li>
<li class="toctree-l3"><a class="reference internal" href="../io/table_provider.html">Custom Table Provider</a></li>
</ul>
</details></li>
<li class="toctree-l2"><a class="reference internal" href="../configuration.html">Configuration</a></li>
<li class="toctree-l2"><a class="reference internal" href="../distributing-work.html">Distributing work</a></li>
<li class="toctree-l2"><a class="reference internal" href="../sql.html">SQL</a></li>
<li class="toctree-l2"><a class="reference internal" href="../upgrade-guides.html">Upgrade Guides</a></li>
<li class="toctree-l2"><a class="reference internal" href="../ai-coding-assistants.html">Using AI Coding Assistants</a></li>
</ul>
</details></li>
<li class="toctree-l1 has-children"><a class="reference internal" href="../../contributor-guide/index.html">Contributor Guide</a><details open="open"><summary><span class="toctree-toggle" role="presentation"><i class="fa-solid fa-chevron-down"></i></span></summary><ul>
<li class="toctree-l2"><a class="reference internal" href="../../contributor-guide/introduction.html">Introduction</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../contributor-guide/ffi.html">Python Extensions</a></li>
</ul>
</details></li>
<li class="toctree-l1 has-children"><a class="reference internal" href="../../autoapi/index.html">API Reference</a><details open="open"><summary><span class="toctree-toggle" role="presentation"><i class="fa-solid fa-chevron-down"></i></span></summary><ul>
<li class="toctree-l2 has-children"><a class="reference internal" href="../../autoapi/datafusion/index.html">datafusion</a><details><summary><span class="toctree-toggle" role="presentation"><i class="fa-solid fa-chevron-down"></i></span></summary><ul>
<li class="toctree-l3"><a class="reference internal" href="../../autoapi/datafusion/catalog/index.html">datafusion.catalog</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../autoapi/datafusion/context/index.html">datafusion.context</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../autoapi/datafusion/dataframe/index.html">datafusion.dataframe</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../autoapi/datafusion/dataframe_formatter/index.html">datafusion.dataframe_formatter</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../autoapi/datafusion/expr/index.html">datafusion.expr</a></li>
<li class="toctree-l3 has-children"><a class="reference internal" href="../../autoapi/datafusion/functions/index.html">datafusion.functions</a><details><summary><span class="toctree-toggle" role="presentation"><i class="fa-solid fa-chevron-down"></i></span></summary><ul>
<li class="toctree-l4"><a class="reference internal" href="../../autoapi/datafusion/functions/spark/index.html">datafusion.functions.spark</a></li>
</ul>
</details></li>
<li class="toctree-l3 has-children"><a class="reference internal" href="../../autoapi/datafusion/input/index.html">datafusion.input</a><details><summary><span class="toctree-toggle" role="presentation"><i class="fa-solid fa-chevron-down"></i></span></summary><ul>
<li class="toctree-l4"><a class="reference internal" href="../../autoapi/datafusion/input/base/index.html">datafusion.input.base</a></li>
<li class="toctree-l4"><a class="reference internal" href="../../autoapi/datafusion/input/location/index.html">datafusion.input.location</a></li>
</ul>
</details></li>
<li class="toctree-l3"><a class="reference internal" href="../../autoapi/datafusion/io/index.html">datafusion.io</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../autoapi/datafusion/ipc/index.html">datafusion.ipc</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../autoapi/datafusion/object_store/index.html">datafusion.object_store</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../autoapi/datafusion/options/index.html">datafusion.options</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../autoapi/datafusion/plan/index.html">datafusion.plan</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../autoapi/datafusion/record_batch/index.html">datafusion.record_batch</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../autoapi/datafusion/substrait/index.html">datafusion.substrait</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../autoapi/datafusion/unparser/index.html">datafusion.unparser</a></li>
<li class="toctree-l3"><a class="reference internal" href="../../autoapi/datafusion/user_defined/index.html">datafusion.user_defined</a></li>
</ul>
</details></li>
</ul>
</details></li>
<li class="toctree-l1 has-children"><a class="reference internal" href="../../links.html">Links</a><details open="open"><summary><span class="toctree-toggle" role="presentation"><i class="fa-solid fa-chevron-down"></i></span></summary><ul>
<li class="toctree-l2"><a class="reference external" href="https://github.com/apache/datafusion-python">GitHub and Issue Tracker</a></li>
<li class="toctree-l2"><a class="reference external" href="https://docs.rs/datafusion/latest/datafusion/">Rust API Docs</a></li>
<li class="toctree-l2"><a class="reference external" href="https://github.com/apache/datafusion/blob/main/CODE_OF_CONDUCT.md">Code of Conduct</a></li>
<li class="toctree-l2"><a class="reference external" href="https://github.com/apache/datafusion-python/tree/main/examples">Examples</a></li>
</ul>
</details></li>
</ul>
</div>
</nav></div>
</div>
<div class="sidebar-primary-items__end sidebar-primary__section">
<div class="sidebar-primary-item">
<div id="ethical-ad-placement"
class="flat"
data-ea-publisher="readthedocs"
data-ea-type="readthedocs-sidebar"
data-ea-manual="true">
</div></div>
</div>
</div>
<main id="main-content" class="bd-main" role="main">
<div class="bd-content">
<div class="bd-article-container">
<div class="bd-header-article d-print-none">
<div class="header-article-items header-article__inner">
<div class="header-article-items__start">
<div class="header-article-item">
<nav aria-label="Breadcrumb" class="d-print-none">
<ul class="bd-breadcrumbs">
<li class="breadcrumb-item breadcrumb-home">
<a href="../../index.html" class="nav-link" aria-label="Home">
<i class="fa-solid fa-home"></i>
</a>
</li>
<li class="breadcrumb-item"><a href="../index.html" class="nav-link">User Guide</a></li>
<li class="breadcrumb-item"><a href="index.html" class="nav-link">DataFrames</a></li>
<li class="breadcrumb-item active" aria-current="page"><span class="ellipsis">DataFrame Rendering</span></li>
</ul>
</nav>
</div>
</div>
</div>
</div>
<div id="searchbox"></div>
<article class="bd-article">
<!---
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
-->
<section id="dataframe-rendering">
<h1>DataFrame Rendering<a class="headerlink" href="#dataframe-rendering" title="Link to this heading">#</a></h1>
<p>DataFusion provides configurable rendering for DataFrames in both plain text and HTML
formats. The <code class="docutils literal notranslate"><span class="pre">datafusion.dataframe_formatter</span></code> module controls how DataFrames are
displayed in Jupyter notebooks (via <code class="docutils literal notranslate"><span class="pre">_repr_html_</span></code>), in the terminal (via <code class="docutils literal notranslate"><span class="pre">__repr__</span></code>),
and anywhere else a string or HTML representation is needed.</p>
<section id="basic-rendering">
<h2>Basic Rendering<a class="headerlink" href="#basic-rendering" title="Link to this heading">#</a></h2>
<p>In a Jupyter environment, displaying a DataFrame triggers HTML rendering:</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="c1"># Will display as HTML table in Jupyter</span>
<span class="n">df</span>
<span class="c1"># Explicit display also uses HTML rendering</span>
<span class="n">display</span><span class="p">(</span><span class="n">df</span><span class="p">)</span>
</pre></div>
</div>
<p>In a terminal or when converting to string, plain text rendering is used:</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="c1"># Plain text table output</span>
<span class="nb">print</span><span class="p">(</span><span class="n">df</span><span class="p">)</span>
</pre></div>
</div>
</section>
<section id="configuring-the-formatter">
<h2>Configuring the Formatter<a class="headerlink" href="#configuring-the-formatter" title="Link to this heading">#</a></h2>
<p>You can customize how DataFrames are rendered by configuring the global formatter:</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span><span class="w"> </span><span class="nn">datafusion.dataframe_formatter</span><span class="w"> </span><span class="kn">import</span> <span class="n">configure_formatter</span>
<span class="n">configure_formatter</span><span class="p">(</span>
<span class="n">max_cell_length</span><span class="o">=</span><span class="mi">25</span><span class="p">,</span> <span class="c1"># Maximum characters in a cell before truncation</span>
<span class="n">max_width</span><span class="o">=</span><span class="mi">1000</span><span class="p">,</span> <span class="c1"># Maximum width in pixels (HTML only)</span>
<span class="n">max_height</span><span class="o">=</span><span class="mi">300</span><span class="p">,</span> <span class="c1"># Maximum height in pixels (HTML only)</span>
<span class="n">max_memory_bytes</span><span class="o">=</span><span class="mi">2097152</span><span class="p">,</span> <span class="c1"># Maximum memory for rendering (2MB)</span>
<span class="n">min_rows</span><span class="o">=</span><span class="mi">10</span><span class="p">,</span> <span class="c1"># Minimum number of rows to display</span>
<span class="n">max_rows</span><span class="o">=</span><span class="mi">10</span><span class="p">,</span> <span class="c1"># Maximum rows to display</span>
<span class="n">enable_cell_expansion</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span> <span class="c1"># Allow expanding truncated cells (HTML only)</span>
<span class="n">custom_css</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="c1"># Additional custom CSS (HTML only)</span>
<span class="n">show_truncation_message</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span> <span class="c1"># Show message when data is truncated</span>
<span class="n">style_provider</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="c1"># Custom styling provider (HTML only)</span>
<span class="n">use_shared_styles</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span> <span class="c1"># Share styles across tables (HTML only)</span>
<span class="p">)</span>
</pre></div>
</div>
<p>The formatter settings affect all DataFrames displayed after configuration.</p>
</section>
<section id="custom-style-providers">
<h2>Custom Style Providers<a class="headerlink" href="#custom-style-providers" title="Link to this heading">#</a></h2>
<p>For HTML styling, you can create a custom style provider that implements the
<code class="docutils literal notranslate"><span class="pre">StyleProvider</span></code> protocol:</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span><span class="w"> </span><span class="nn">datafusion.dataframe_formatter</span><span class="w"> </span><span class="kn">import</span> <span class="n">configure_formatter</span>
<span class="k">class</span><span class="w"> </span><span class="nc">MyStyleProvider</span><span class="p">:</span>
<span class="k">def</span><span class="w"> </span><span class="nf">get_cell_style</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="w"> </span><span class="sd">&quot;&quot;&quot;Return CSS style string for table data cells.&quot;&quot;&quot;</span>
<span class="k">return</span> <span class="s2">&quot;border: 1px solid #ddd; padding: 8px; text-align: left;&quot;</span>
<span class="k">def</span><span class="w"> </span><span class="nf">get_header_style</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="w"> </span><span class="sd">&quot;&quot;&quot;Return CSS style string for table header cells.&quot;&quot;&quot;</span>
<span class="k">return</span> <span class="p">(</span>
<span class="s2">&quot;background-color: #007bff; color: white; &quot;</span>
<span class="s2">&quot;padding: 8px; text-align: left;&quot;</span>
<span class="p">)</span>
<span class="c1"># Apply the custom style provider</span>
<span class="n">configure_formatter</span><span class="p">(</span><span class="n">style_provider</span><span class="o">=</span><span class="n">MyStyleProvider</span><span class="p">())</span>
</pre></div>
</div>
</section>
<section id="custom-cell-formatters">
<h2>Custom Cell Formatters<a class="headerlink" href="#custom-cell-formatters" title="Link to this heading">#</a></h2>
<p>You can register custom formatters for specific Python types. A cell formatter is any
callable that takes a value and returns a string:</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span><span class="w"> </span><span class="nn">datafusion.dataframe_formatter</span><span class="w"> </span><span class="kn">import</span> <span class="n">get_formatter</span>
<span class="n">formatter</span> <span class="o">=</span> <span class="n">get_formatter</span><span class="p">()</span>
<span class="c1"># Format floats to 2 decimal places</span>
<span class="n">formatter</span><span class="o">.</span><span class="n">register_formatter</span><span class="p">(</span><span class="nb">float</span><span class="p">,</span> <span class="k">lambda</span> <span class="n">v</span><span class="p">:</span> <span class="sa">f</span><span class="s2">&quot;</span><span class="si">{</span><span class="n">v</span><span class="si">:</span><span class="s2">.2f</span><span class="si">}</span><span class="s2">&quot;</span><span class="p">)</span>
<span class="c1"># Format dates in a custom way</span>
<span class="kn">from</span><span class="w"> </span><span class="nn">datetime</span><span class="w"> </span><span class="kn">import</span> <span class="n">date</span>
<span class="n">formatter</span><span class="o">.</span><span class="n">register_formatter</span><span class="p">(</span><span class="n">date</span><span class="p">,</span> <span class="k">lambda</span> <span class="n">v</span><span class="p">:</span> <span class="n">v</span><span class="o">.</span><span class="n">strftime</span><span class="p">(</span><span class="s2">&quot;%B </span><span class="si">%d</span><span class="s2">, %Y&quot;</span><span class="p">))</span>
</pre></div>
</div>
</section>
<section id="custom-cell-and-header-builders">
<h2>Custom Cell and Header Builders<a class="headerlink" href="#custom-cell-and-header-builders" title="Link to this heading">#</a></h2>
<p>For full control over the HTML of individual cells or headers, you can set custom
builder functions:</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span><span class="w"> </span><span class="nn">datafusion.dataframe_formatter</span><span class="w"> </span><span class="kn">import</span> <span class="n">get_formatter</span>
<span class="n">formatter</span> <span class="o">=</span> <span class="n">get_formatter</span><span class="p">()</span>
<span class="c1"># Custom cell builder receives (value, row, col, table_id) and returns HTML</span>
<span class="k">def</span><span class="w"> </span><span class="nf">my_cell_builder</span><span class="p">(</span><span class="n">value</span><span class="p">,</span> <span class="n">row</span><span class="p">,</span> <span class="n">col</span><span class="p">,</span> <span class="n">table_id</span><span class="p">):</span>
<span class="n">color</span> <span class="o">=</span> <span class="s2">&quot;red&quot;</span> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">value</span><span class="p">,</span> <span class="p">(</span><span class="nb">int</span><span class="p">,</span> <span class="nb">float</span><span class="p">))</span> <span class="ow">and</span> <span class="n">value</span> <span class="o">&lt;</span> <span class="mi">0</span> <span class="k">else</span> <span class="s2">&quot;black&quot;</span>
<span class="k">return</span> <span class="sa">f</span><span class="s2">&quot;&lt;td style=&#39;color: </span><span class="si">{</span><span class="n">color</span><span class="si">}</span><span class="s2">; padding: 8px;&#39;&gt;</span><span class="si">{</span><span class="n">value</span><span class="si">}</span><span class="s2">&lt;/td&gt;&quot;</span>
<span class="n">formatter</span><span class="o">.</span><span class="n">set_custom_cell_builder</span><span class="p">(</span><span class="n">my_cell_builder</span><span class="p">)</span>
<span class="c1"># Custom header builder receives a schema field and returns HTML</span>
<span class="k">def</span><span class="w"> </span><span class="nf">my_header_builder</span><span class="p">(</span><span class="n">field</span><span class="p">):</span>
<span class="k">return</span> <span class="sa">f</span><span class="s2">&quot;&lt;th style=&#39;background: #333; color: white; padding: 8px;&#39;&gt;</span><span class="si">{</span><span class="n">field</span><span class="o">.</span><span class="n">name</span><span class="si">}</span><span class="s2">&lt;/th&gt;&quot;</span>
<span class="n">formatter</span><span class="o">.</span><span class="n">set_custom_header_builder</span><span class="p">(</span><span class="n">my_header_builder</span><span class="p">)</span>
</pre></div>
</div>
</section>
<section id="performance-optimization-with-shared-styles">
<h2>Performance Optimization with Shared Styles<a class="headerlink" href="#performance-optimization-with-shared-styles" title="Link to this heading">#</a></h2>
<p>The <code class="docutils literal notranslate"><span class="pre">use_shared_styles</span></code> parameter (enabled by default) optimizes performance when
displaying multiple DataFrames in notebook environments:</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span><span class="w"> </span><span class="nn">datafusion.dataframe_formatter</span><span class="w"> </span><span class="kn">import</span> <span class="n">configure_formatter</span>
<span class="c1"># Default: Use shared styles (recommended for notebooks)</span>
<span class="n">configure_formatter</span><span class="p">(</span><span class="n">use_shared_styles</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
<span class="c1"># Disable shared styles (each DataFrame includes its own styles)</span>
<span class="n">configure_formatter</span><span class="p">(</span><span class="n">use_shared_styles</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span>
</pre></div>
</div>
<p>When <code class="docutils literal notranslate"><span class="pre">use_shared_styles=True</span></code>:</p>
<ul class="simple">
<li><p>CSS styles and JavaScript are included only once per notebook session</p></li>
<li><p>This reduces HTML output size and prevents style duplication</p></li>
<li><p>Improves rendering performance with many DataFrames</p></li>
<li><p>Applies consistent styling across all DataFrames</p></li>
</ul>
</section>
<section id="working-with-the-formatter-directly">
<h2>Working with the Formatter Directly<a class="headerlink" href="#working-with-the-formatter-directly" title="Link to this heading">#</a></h2>
<p>You can use <code class="docutils literal notranslate"><span class="pre">get_formatter()</span></code> and <code class="docutils literal notranslate"><span class="pre">set_formatter()</span></code> for direct access to the global
formatter instance:</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span><span class="w"> </span><span class="nn">datafusion.dataframe_formatter</span><span class="w"> </span><span class="kn">import</span> <span class="p">(</span>
<span class="n">DataFrameHtmlFormatter</span><span class="p">,</span>
<span class="n">get_formatter</span><span class="p">,</span>
<span class="n">set_formatter</span><span class="p">,</span>
<span class="p">)</span>
<span class="c1"># Get and modify the current formatter</span>
<span class="n">formatter</span> <span class="o">=</span> <span class="n">get_formatter</span><span class="p">()</span>
<span class="nb">print</span><span class="p">(</span><span class="n">formatter</span><span class="o">.</span><span class="n">max_rows</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="n">formatter</span><span class="o">.</span><span class="n">max_cell_length</span><span class="p">)</span>
<span class="c1"># Create and set a fully custom formatter</span>
<span class="n">custom_formatter</span> <span class="o">=</span> <span class="n">DataFrameHtmlFormatter</span><span class="p">(</span>
<span class="n">max_cell_length</span><span class="o">=</span><span class="mi">50</span><span class="p">,</span>
<span class="n">max_rows</span><span class="o">=</span><span class="mi">20</span><span class="p">,</span>
<span class="n">enable_cell_expansion</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span>
<span class="p">)</span>
<span class="n">set_formatter</span><span class="p">(</span><span class="n">custom_formatter</span><span class="p">)</span>
</pre></div>
</div>
<p>Reset to default formatting:</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span><span class="w"> </span><span class="nn">datafusion.dataframe_formatter</span><span class="w"> </span><span class="kn">import</span> <span class="n">reset_formatter</span>
<span class="c1"># Reset to default settings</span>
<span class="n">reset_formatter</span><span class="p">()</span>
</pre></div>
</div>
</section>
<section id="memory-and-display-controls">
<h2>Memory and Display Controls<a class="headerlink" href="#memory-and-display-controls" title="Link to this heading">#</a></h2>
<p>You can control how much data is displayed and how much memory is used for rendering:</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span><span class="w"> </span><span class="nn">datafusion.dataframe_formatter</span><span class="w"> </span><span class="kn">import</span> <span class="n">configure_formatter</span>
<span class="n">configure_formatter</span><span class="p">(</span>
<span class="n">max_memory_bytes</span><span class="o">=</span><span class="mi">4</span> <span class="o">*</span> <span class="mi">1024</span> <span class="o">*</span> <span class="mi">1024</span><span class="p">,</span> <span class="c1"># 4MB maximum memory for display</span>
<span class="n">min_rows</span><span class="o">=</span><span class="mi">20</span><span class="p">,</span> <span class="c1"># Always show at least 20 rows</span>
<span class="n">max_rows</span><span class="o">=</span><span class="mi">50</span><span class="p">,</span> <span class="c1"># Show up to 50 rows in output</span>
<span class="p">)</span>
</pre></div>
</div>
<p>These parameters help balance comprehensive data display against performance considerations.</p>
</section>
<section id="best-practices">
<h2>Best Practices<a class="headerlink" href="#best-practices" title="Link to this heading">#</a></h2>
<ol class="arabic simple">
<li><p><strong>Global Configuration</strong>: Use <code class="docutils literal notranslate"><span class="pre">configure_formatter()</span></code> at the beginning of your notebook to set up consistent formatting for all DataFrames.</p></li>
<li><p><strong>Memory Management</strong>: Set appropriate <code class="docutils literal notranslate"><span class="pre">max_memory_bytes</span></code> limits to prevent performance issues with large datasets.</p></li>
<li><p><strong>Shared Styles</strong>: Keep <code class="docutils literal notranslate"><span class="pre">use_shared_styles=True</span></code> (default) for better performance in notebooks with multiple DataFrames.</p></li>
<li><p><strong>Reset When Needed</strong>: Call <code class="docutils literal notranslate"><span class="pre">reset_formatter()</span></code> when you want to start fresh with default settings.</p></li>
<li><p><strong>Cell Expansion</strong>: Use <code class="docutils literal notranslate"><span class="pre">enable_cell_expansion=True</span></code> when cells might contain longer content that users may want to see in full.</p></li>
</ol>
</section>
<section id="additional-resources">
<h2>Additional Resources<a class="headerlink" href="#additional-resources" title="Link to this heading">#</a></h2>
<ul class="simple">
<li><p><a class="reference internal" href="index.html"><span class="doc">DataFrames</span></a> - Complete guide to using DataFrames</p></li>
<li><p><a class="reference internal" href="../io/index.html"><span class="doc">IO</span></a> - I/O Guide for reading data from various sources</p></li>
<li><p><a class="reference internal" href="../data-sources.html"><span class="doc">Data Sources</span></a> - Comprehensive data sources guide</p></li>
<li><p><a class="reference internal" href="../io/csv.html#io-csv"><span class="std std-ref">CSV</span></a> - CSV file reading</p></li>
<li><p><a class="reference internal" href="../io/parquet.html#io-parquet"><span class="std std-ref">Parquet</span></a> - Parquet file reading</p></li>
<li><p><a class="reference internal" href="../io/json.html#io-json"><span class="std std-ref">JSON</span></a> - JSON file reading</p></li>
<li><p><a class="reference internal" href="../io/avro.html#io-avro"><span class="std std-ref">Avro</span></a> - Avro file reading</p></li>
<li><p><a class="reference internal" href="../io/table_provider.html#io-custom-table-provider"><span class="std std-ref">Custom Table Provider</span></a> - Custom table providers</p></li>
<li><p><a class="reference external" href="https://arrow.apache.org/datafusion-python/api/index.html">API Reference</a> - Full API reference</p></li>
</ul>
</section>
</section>
</article>
<footer class="prev-next-footer d-print-none">
<div class="prev-next-area">
<a class="left-prev"
href="index.html"
title="previous page">
<i class="fa-solid fa-angle-left"></i>
<div class="prev-next-info">
<p class="prev-next-subtitle">previous</p>
<p class="prev-next-title">DataFrames</p>
</div>
</a>
<a class="right-next"
href="execution-metrics.html"
title="next page">
<div class="prev-next-info">
<p class="prev-next-subtitle">next</p>
<p class="prev-next-title">Execution Metrics</p>
</div>
<i class="fa-solid fa-angle-right"></i>
</a>
</div>
</footer>
</div>
<dialog id="pst-secondary-sidebar-modal"></dialog>
<div id="pst-secondary-sidebar" class="bd-sidebar-secondary bd-toc"><div class="sidebar-secondary-items sidebar-secondary__inner">
<div class="sidebar-secondary-item">
<div
id="pst-page-navigation-heading-2"
class="page-toc tocsection onthispage">
<i class="fa-solid fa-list"></i> On this page
</div>
<nav class="bd-toc-nav page-toc" aria-labelledby="pst-page-navigation-heading-2">
<ul class="visible nav section-nav flex-column">
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#basic-rendering">Basic Rendering</a></li>
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#configuring-the-formatter">Configuring the Formatter</a></li>
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#custom-style-providers">Custom Style Providers</a></li>
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#custom-cell-formatters">Custom Cell Formatters</a></li>
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#custom-cell-and-header-builders">Custom Cell and Header Builders</a></li>
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#performance-optimization-with-shared-styles">Performance Optimization with Shared Styles</a></li>
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#working-with-the-formatter-directly">Working with the Formatter Directly</a></li>
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#memory-and-display-controls">Memory and Display Controls</a></li>
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#best-practices">Best Practices</a></li>
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#additional-resources">Additional Resources</a></li>
</ul>
</nav></div>
</div></div>
</div>
<footer class="bd-footer-content">
</footer>
</main>
</div>
</div>
<!-- Scripts loaded after <body> so the DOM is not blocked -->
<script defer src="../../_static/scripts/bootstrap.js?digest=8878045cc6db502f8baf"></script>
<script defer src="../../_static/scripts/pydata-sphinx-theme.js?digest=8878045cc6db502f8baf"></script>
<!-- Based on pydata_sphinx_theme/footer.html -->
<footer class="footer mt-5 mt-md-0">
<div class="container">
<div class="footer-item">
<p>Apache Arrow DataFusion, Arrow DataFusion, Apache, the Apache feather logo, and the Apache Arrow DataFusion project logo</p>
<p>are either registered trademarks or trademarks of The Apache Software Foundation in the United States and other countries.</p>
</div>
</div>
</footer>
</body>
</html>