jupyter-xeus / xeus-cling

Jupyter kernel for the C++ programming language
BSD 3-Clause "New" or "Revised" License
3.08k stars 299 forks source link

HTML Formatting in Notebook #240

Closed vishwesh5 closed 5 years ago

vishwesh5 commented 5 years ago

Hi everyone

Is it possible to load HTML styling in a Xeus Cling kernel notebook? For example, I am doing something like this in python:

image

Is there a Xeus Cling equivalent for this? Or is there a word around?

Thanks in advance

Vishwesh

martinRenou commented 5 years ago

I guess you could create your own HTML class, a bit like in the Notebook example with the image class: https://github.com/QuantStack/xeus-cling/blob/master/notebooks/xcpp.ipynb

    struct html
    {   
        inline html(const std::string& value)
        {
            m_value = value;
        }

        std::string m_value;
    };

    xeus::xjson mime_bundle_repr(const html& h)
    {
        auto bundle = xeus::xjson::object();
        bundle["text/html"] = h.m_value;
        return bundle;
    }

That could be a first option. Another option could be to use https://github.com/QuantStack/xwidgets, it has an html widget class (https://github.com/QuantStack/xwidgets/blob/master/include/xwidgets/xhtml.hpp). That would be a more "interactive" option, because you can update the HTML value dynamically from C++ and the visual output will update itself automatically.

vishwesh5 commented 5 years ago

Thanks @martinRenou ! By any chance, is there an example for xhtml?

martinRenou commented 5 years ago

Yes! In this Notebook: https://github.com/QuantStack/xwidgets/blob/master/notebooks/xwidgets.ipynb if you look for HTML on the page you will see it

vishwesh5 commented 5 years ago

This worked! image

Thanks a lot!

One final question. In the image, I had to write the entire html styling code. Is it possible to load it from a file? My plan is to maintain a central styling file which can be loaded by all the c++ notebooks.

martinRenou commented 5 years ago

Maybe look for ways to load a file into an std::string, something like https://stackoverflow.com/questions/2602013/read-whole-ascii-file-into-c-stdstring

vishwesh5 commented 5 years ago

Thanks for the reference! I was able to load the html style in a string buffer.str()

image

Then I did this:

image

Am I missing something? I am pretty sure I am

martinRenou commented 5 years ago

I am not sure xhtml( at the beginning and )xhtml at the end are needed, can you try removing them?

vishwesh5 commented 5 years ago

You are amazing! It worked!

Thanks a lot for your help!

martinRenou commented 5 years ago

Ahah, happy to help :) I'm closing this. Don't hesitate to ask other questions though.

ghost commented 1 year ago

Does xw::html work for css and html only? It doesn't seem to run javascript although my notebook is trusted