Skip to content

Callout boxes for ipynb output #1167

Open

Description

@jjallaire

Discussed in #1166

Originally posted by linogaliana June 17, 2022
Hi,

callouts are a really nice feature. I have seen in the doc that currently Jupyter Notebooks are not supported (https://quarto.org/docs/authoring/callouts.html). I totally understand this is not a priority and might be tricky to implement. However, in the future, that would be really cool to also propose boxes for this output format.

  • As far as I understood discussions in Proper way to add "alert"/"note"/"hint"/"warning"/... boxes? jupyter/notebook#1292 it seems that there exists some features in Jupyter Notebook with some raw HTML <div>. Some examples are given here
  • However, all boxes provided by quarto are not supported
  • Boxes are not as aesthetic as those provided by quarto (or jupyterbook). This is of course not a priority (functional boxes would first be great) but this would be cool to a nice box in notebook. I saw that it is possible to embed some styling element directly in the div (see this stackoverflow thread) but maybe there's a better way to start
  • @choldgraf maybe has an opinion on the best way to proceed

Once again, I understand that tweaking some notebook features is not a priority. But helping python users generate nice notebooks from quarto could be appreciated

Activity

  1. jjallaire commented on Jun 17, 2022

    @jjallaire
    CollaboratorAuthor

    Yes, we should definitely be able to get this to work! We have some other work on ipynb output we'd like to do soon and will take care of this at that time.

  2. added this to the Future milestone on Jun 17, 2022
  3. diego898 commented on Mar 1, 2023

    @diego898

    I think this would be great - right now authoring in jupyter notebooks leaves

    ::: {.callout-note}
    

    visible everywhere in the notebook.

  4. diego898 commented on Mar 4, 2023

    @diego898

    Also, I think it would be a good feature to add support for a title property in a fallout vox to avoid using headers. Right now, headers inside calloutboxes serve as title for the box, but “hijack” a table of contents/breadcrumbs unnecessarily.

    should this be a separate feature request? Thank you!

  5. jjallaire commented on Mar 5, 2023

    @jjallaire
    CollaboratorAuthor

    Just added support for title here: 525cb56

    Should be available in an updated pre-release build later on today.

  6. linogaliana commented on Jul 17, 2023

    @linogaliana

    If it can be of any help, currently I am doing, for instance, this hack:

    ::: {.cell .markdown}
    ```{=html}
    <div class="alert alert-info" role="alert">
    <h3 class="alert-heading"><i class="fa-solid fa-comment"></i> Note</h3>
    ```
    
    
    Dans un processus de production, où normalement on connait les types des variables du `DataFrame` qu'on va importer,
    il convient de préciser les types avec lesquels on souhaite importer les données
    (argument `dtype`, sous la forme d'un dictionnaire).
    Cela est particulièrement important lorsqu'on désire utiliser une colonne
    comme une variable textuelle mais qu'elle comporte des attributs proches d'un nombre
    qui vont inciter `pandas` à l'importer sous forme de variable numérique.
    
    Par exemple, une colonne `[00001,00002,...] ` risque d'être importée comme une variable numérique, ignorant l'information des premiers 0 (qui peuvent pourtant la distinguer de la séquence 1, 2, etc.). Pour s'assurer que `pandas` importe sous forme textuelle la variable, on peut utiliser `dtype = {"code": "str"}`
    Sinon, on peut importer le csv, et modifier les types avec `astype()`.
    Avec `astype`, on peut gérer les erreurs de conversion avec le paramètre `errors`.
    
    ```{=html}
    </div>
    ```
    :::

    An example can be found here. More generally I have built different boxes based on alert divs in Jupyter and I can share more if it helps, for instance the associated CSS.

    This is what looks like the box on my jupterlab instance, after rendering the file using ipynb format :

    image

    There is still some work to get ir nicely rendered on google colab, there are probably some parameters that override my hacks

  7. added
    enhancement
    engines-jupyter
    callouts
    on Apr 28, 2024
  8. linogaliana commented on Sep 26, 2024

    @linogaliana

    I managed to do something, not perfect but functional using lua. I used it for a few months and forgot to share it here.

    My solution

    Basically, my solution has been to create special divs to handle boxes: my lua script recognizes them and

    • for ipynb output, it replaces with appropriate html code (proposition below)
    • for other format, it replaces with the appropriate callout call that should be used when using quarto

    In other words, in my quarto input document (.qmd), to create a note callout I would do

    ::: {.note}
    ## Note title
    
    Note content
    :::
    

    instead of

    ::: {.callout-note}
    ## Note title
    
    Note content
    :::
    

    This special div is not perfect, it is a workaround, that I would be happy to avoid if Quarto integrates a solution for callout-* in ipynb format.

    My lua script injects the appropriate html tags around the content of the box. Ideally, I would use classes like callout callout-style-default callout-note etc. but I noticed that these classes do not render well on my JupyterLab instance (cloud version with quarto extension installed). So I had to go for bootstrap alert classes that render better on jupyterlab.

    My lua filter

    The most important pieces are this one:

        local customIcons = {
          exercise = "<i class=\"fa-solid fa-pencil\"></i>",
          note = "<i class=\"fa-solid fa-comment\"></i>",
          tip = "<i class=\"fa-solid fa-lightbulb\"></i>",
          warning = "<i class=\"fa-solid fa-triangle-exclamation\"></i>",
          important =  "<i class=\"fa-solid fa-triangle-exclamation\"></i>"
        }
        local customColor = {
            note = "info",
            tip = "success",
            exercise = "success",
            warning = "danger",
            important = "danger"
        }

    that creates a mapping between callout names and alert classes and that one:

        if quarto.doc.is_format("ipynb") then
          if type == "exercise" then
            type = "tip"
            icon = customIcons["exercise"]
          end
          -- Add html formatting around
          local headBox = "<div class=\"alert alert-" .. color .. "\" role=\"alert\">\n" ..
            "<h3 class=\"alert-heading\">" .. icon .. " " .. title .. "</h3>\n"
      
          -- Insert raw HTML blocks
          div.content:insert(1, pandoc.RawBlock('html', headBox .. "\n"))
          div.content:insert(pandoc.RawBlock('html', "\n</div>"))   
          return div

    What it looks like

    As an example, this text

    :::: {.tip}
    ## Skills to be acquired by the end of this chapter
    
    - Import a dataset as a `Pandas` dataframe and explore its structure;
    - Perform manipulations on columns and rows;
    - Construct aggregate statistics and chain operations;
    - Use `Pandas` graphical methods to quickly represent data distribution.
    
    ::::

    will become like that in the final output (when doing quarto render toto.qmd --to ipynb) when using the filter

    <div class="alert alert-success" role="alert">
    <h3 class="alert-heading"><i class="fa-solid fa-lightbulb"></i> Skills to be acquired by the end of this chapter</h3>
    
    -   Import a dataset as a `Pandas` dataframe and explore its structure;
    -   Perform manipulations on columns and rows;
    -   Construct aggregate statistics and chain operations;
    -   Use `Pandas` graphical methods to quickly represent data distribution.
    
    </div>
    

    You can see an example of rendering in Github UI here.

    In VSCode (server version with quarto extension enabled) it looks like

    image

    In JupyterLab (server version with quarto extension enabled) it looks like

    image

    This does the job, except on colab that looks like supercharging jupyterlab with css that makes alert box non visible.

    How it could be even better

    But I'm a maniac. I would like to have nice callouts as would be in html output. I managed to do that with callout classes but only in VSCode (I don't know if it comes from quarto extension or a natural support of that class in vscode). My problem is that does not render well in Jupyter, which makes me terribly sad. Still I share that here if you are interested in that solution:

    My script to get nice callout in vscode is here. This time, I could inject the callout classes

        local headBox = "<div class=\"callout callout-style-default callout-" .. type .. " callout-titled\">\n" ..
          "<div class=\"callout-header d-flex align-content-center\">\n" ..
          "<div class=\"callout-icon-container\">" ..
          "<i class=\"callout-icon\"></i>\n" ..
          "</div>\n"
    

    In vscode, this renders like that:

    image

    (you can see the difference between an alert box (the first one) and a callout in this image (the second one))

    unfortunately in jupyter this gives

    image

    By the way you can see an example of rendering in Github UI here. It does not render well but I think it does not matter: we want the notebook to be beautiful in our IDE, not in jupyter

    Hope these elements help

  9. self-assigned this
    on Oct 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

calloutsengines-jupyterenhancement

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions