Skip to content

Commit

Permalink
Keep image cell size when re-rendering into existing cell. Add rudime…
Browse files Browse the repository at this point in the history
…ntary support for plotly graphs.
  • Loading branch information
Kasper Peeters committed Nov 11, 2024
1 parent af5955b commit 4535a24
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 36 deletions.
13 changes: 13 additions & 0 deletions core/cadabra2_defaults.py.in
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,14 @@ try:
except ImportError:
have_matplotlib=False

have_plotly=True
try:
import plotly
import plotly.graph_objs
except ImportError:
have_plotly=False


def save_history(history_path):
try:
readline.write_history_file(history_path)
Expand Down Expand Up @@ -249,6 +257,11 @@ def display(obj, cell_id=0, delay_send=False):
b64 = base64.b64encode(imgstring.getvalue())
return server.send(b64, "image_svg", 0, cell_id, False)

elif have_plotly and isinstance(obj, plotly.graph_objs._figure.Figure):
imgstring = obj.to_image(format="svg")
b64 = base64.b64encode(imgstring)
return server.send(b64, "image_svg", 0, cell_id, False)

elif hasattr(obj,'_backend'):
# print("figure 3", file=sys.stderr)
if hasattr(obj._backend,'fig'):
Expand Down
30 changes: 14 additions & 16 deletions core/packages/cdb/graphics/plot.cnb

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion frontend/gtkmm/CodeInput.cc
Original file line number Diff line number Diff line change
Expand Up @@ -533,8 +533,10 @@ bool CodeInput::exp_input_tv::on_key_press_event(GdkEventKey* event)
return true;
}
else if(is_tab) {
// If one or more lines are selected, indent the whole block.
// FIXME: implement

// Only complete if the last character is not whitespace.

Glib::RefPtr<Gtk::TextBuffer::Mark> ins = get_buffer()->get_insert();
Gtk::TextBuffer::iterator it=textbuf->get_iter_at_mark(ins);
int ipos=textbuf->get_slice(textbuf->begin(), it).bytes();
Expand Down
44 changes: 25 additions & 19 deletions frontend/gtkmm/ImageView.cc
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,10 @@ bool ImageView::on_motion_notify_event(GdkEventMotion *event)
// std::cerr << event->x << ", " << event->y << std::endl;
if(sizing) {
auto cw = width_at_press + (event->x - prev_x);
auto ratio = pixbuf->get_width() / ((double)pixbuf->get_height());
image.set(pixbuf->scale_simple(cw, cw/ratio, Gdk::INTERP_BILINEAR));
set_size_request( cw, cw/ratio );
rerender(cw);
// auto ratio = pixbuf->get_width() / ((double)pixbuf->get_height());
// image.set(pixbuf->scale_simple(cw, cw/ratio, Gdk::INTERP_BILINEAR));
// set_size_request( cw, cw/ratio );
}
return true;
}
Expand Down Expand Up @@ -62,33 +63,38 @@ bool ImageView::on_button_release_event(GdkEventButton *event)

void ImageView::set_image_from_base64(const std::string& b64)
{
auto str = Gio::MemoryInputStream::create();

// The data is ok:
// std::ofstream tst("out2.png");
// tst << Glib::Base64::decode(b64);
// tst.close();

std::string dec=Glib::Base64::decode(b64);
str->add_data(dec.c_str(), dec.size());

pixbuf = Gdk::Pixbuf::create_from_stream_at_scale(str, 400*scale, -1, true);
if(!pixbuf)
std::cerr << "cadabra-client: unable to create image from data" << std::endl;
else {
image.set(pixbuf);
image.set_size_request( pixbuf->get_width(), pixbuf->get_height() );
}
decoded=Glib::Base64::decode(b64);
is_raster=true;
rerender();
}

void ImageView::set_image_from_svg(const std::string& svg)
{
decoded=Glib::Base64::decode(svg);
is_raster=false;
rerender();
}

void ImageView::rerender(int override_width)
{
auto str = Gio::MemoryInputStream::create();
std::string dec=Glib::Base64::decode(svg);
str->add_data(dec.c_str(), dec.size());
pixbuf = Gdk::Pixbuf::create_from_stream_at_scale(str, 400*scale, -1, true);
str->add_data(decoded.c_str(), decoded.size());

int curwidth=400*scale;
if(pixbuf)
curwidth = pixbuf->get_width();
if(override_width!=0)
curwidth=override_width;

pixbuf = Gdk::Pixbuf::create_from_stream_at_scale(str, curwidth, -1, true);

if(!pixbuf)
std::cerr << "cadabra-client: unable to create image from svg data" << std::endl;
std::cerr << "cadabra-client: unable to create image from data" << std::endl;
else {
image.set(pixbuf);
image.set_size_request( pixbuf->get_width(), pixbuf->get_height() );
Expand Down
7 changes: 7 additions & 0 deletions frontend/gtkmm/ImageView.hh
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ namespace cadabra {
virtual bool on_button_release_event(GdkEventButton *event) override;

private:
std::string decoded; // raw byte content of image
bool is_raster;

Gtk::VBox vbox;
Gtk::Image image;
Glib::RefPtr<Gdk::Pixbuf> pixbuf;
Expand All @@ -32,6 +35,10 @@ namespace cadabra {
double prev_x, prev_y;
int height_at_press, width_at_press;
double scale;

// Re-render the image from the raw bytes at the current
// resolution of the image widget (or 400 if not set).
void rerender(int override_width=0);
};

};
1 change: 1 addition & 0 deletions web2/cadabra2/source/changelog.html
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ <h3>github devel branch (2.5.9)</h3>
<li>Fix display of LaTeXForm properties.</li>
<li>Documentation updates.</li>
<li>Close LaTeX input cells on running them (configurable).</li>
<li>Cleanup and document kernel options.</li>
</ul>

<a name="2.5.8"></a>
Expand Down

0 comments on commit 4535a24

Please sign in to comment.