Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resizing images and videos in a jupyter notebook #1312

Closed
wants to merge 33 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
d9b487e
more ipymagic
kolibril13 Apr 10, 2021
f5255ef
added config
kolibril13 Apr 11, 2021
803f7d4
Merge branch 'master' into jupyter_widgets
kolibril13 Apr 11, 2021
5315beb
updated default settings
kolibril13 Apr 11, 2021
69ea887
Merge branch 'master' into jupyter_widgets
kolibril13 Apr 13, 2021
48a6585
Updating dependencies
Darylgolden Apr 15, 2021
2710894
Delete Untitled.ipynb
kolibril13 Apr 15, 2021
684c1c0
Merge pull request #3 from Darylgolden/ipywidgets
kolibril13 Apr 15, 2021
13a1d31
Merge branch 'master' into jupyter_widgets
kolibril13 Apr 15, 2021
c8033b2
Apply suggestions from code review
kolibril13 Apr 15, 2021
9df638e
Merge branch 'master' into jupyter_widgets
kolibril13 Apr 15, 2021
fe5e6eb
Made ipywidgets an extra, updated poetry.lock
jsonvillanueva Apr 17, 2021
4cda642
Merge branch 'master' into jupyter_widgets
kolibril13 Apr 17, 2021
9b52425
Add ipywidgets import to try/except
jsonvillanueva Apr 18, 2021
62352bc
Merge branch 'jupyter_widgets' of github.com:kolibril13/manim into jupy
jsonvillanueva Apr 18, 2021
932f3bf
Merge branch 'master' into jupyter_widgets
kolibril13 Apr 18, 2021
de75561
Merge branch 'master' into jupyter_widgets
kolibril13 Apr 18, 2021
a3a111e
Merge branch 'master' into jupyter_widgets
kolibril13 Apr 19, 2021
2855505
Merge branch 'master' into jupyter_widgets
kolibril13 Apr 27, 2021
28f472a
Merge branch 'master' into jupyter_widgets
kolibril13 May 15, 2021
f0ed424
Merge branch 'master' into jupyter_widgets
kolibril13 May 15, 2021
f76296d
Merge branch 'master' into jupyter_widgets
kolibril13 May 21, 2021
2ba32c4
Merge branch 'master' into jupyter_widgets
kolibril13 May 31, 2021
27b26af
Merge branch 'main' into jupyter_widgets
kolibril13 Jun 8, 2021
afeea8a
Merge branch 'main' into jupyter_widgets
kolibril13 Jun 11, 2021
ba104e0
Hello
kolibril13 Jun 18, 2021
7f22e12
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jun 18, 2021
d0a04c7
Merge branch 'main' into jupyter_widgets
kolibril13 Aug 3, 2021
7c9e8ef
Merge branch 'main' into jupyter_widgets
kolibril13 Aug 13, 2021
e8dab9e
Merge branch 'main' into jupyter_widgets
kolibril13 Sep 19, 2021
e5848e2
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Sep 19, 2021
525b9d4
Merge branch 'main' into jupyter_widgets
kolibril13 Sep 25, 2021
c48cfd2
Merge branch 'main' into jupyter_widgets
kolibril13 Oct 15, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
129 changes: 115 additions & 14 deletions manim/utils/ipython_magic.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import mimetypes
import os
import shutil
import webbrowser
from datetime import datetime
from pathlib import Path
from typing import Any, Dict, List
Expand All @@ -11,6 +12,113 @@
from manim.__main__ import main
from manim.renderer.shader import shader_program_cache


def init_buttons():
b0 = Button(
description="",
icon="fa-picture-o",
button_style="",
layout=Layout(height="30px", width="40px"),
)
b1 = Button(
description="",
icon="fa-expand",
button_style="",
layout=Layout(height="30px", width="40px"),
)
b2 = Button(
description="",
icon="fa-download",
button_style="",
layout=Layout(height="30px", width="40px"),
)
return b0, b1, b2


def image_viewer(image_path, small_width, large_width):

original1t1_width = f"{config.frame_size[0]}px"
file = open(image_path, "rb")
image = file.read()
dis_img = widgets.Image(value=image, format="png")

button_list = GridspecLayout(3, 1, height="120px")
b0, b1, b2 = init_buttons()
button_list[0, 0] = b0
button_list[1, 0] = b1
button_list[2, 0] = b2
dis_img.width = large_width # # default width

def on_button_image1t1_clicked(b):
dis_img.width = original1t1_width

def on_button_expand_clicked(b):
if dis_img.width != small_width:
dis_img.width = small_width
else:
dis_img.width = large_width
kolibril13 marked this conversation as resolved.
Show resolved Hide resolved

def on_button_download_clicked(b):
url = (Path.cwd() / image_path).as_uri()
webbrowser.open(url)

b0.on_click(on_button_image1t1_clicked)
b1.on_click(on_button_expand_clicked)
b2.on_click(on_button_download_clicked)
return AppLayout(
left_sidebar=button_list,
center=dis_img,
pane_widths=["50px", 1, 0],
)


def video_viewer(video_path, small_width, large_width):

original1t1_width = f"{config.frame_size[0]}px"

dis_video = Video.from_file(video_path)
dis_video.controls = True

dis_but = widgets.ToggleButton(
value=False,
description="Fullscreen",
disabled=False,
button_style="",
tooltip="Description",
)

b0, b1, b2 = init_buttons()

button_list = GridspecLayout(3, 1, height="120px")
button_list[0, 0] = b0
button_list[1, 0] = b1
button_list[2, 0] = b2

dis_video.width = large_width # default width

def on_button_image1t1_clicked(b):
dis_video.width = original1t1_width

def on_button_expand_clicked(b):
if dis_video.width != small_width:
dis_video.width = small_width
else:
dis_video.width = large_width
kolibril13 marked this conversation as resolved.
Show resolved Hide resolved

def on_button_download_clicked(b):
url = (Path.cwd() / video_path).as_uri()
webbrowser.open(url)

b0.on_click(on_button_image1t1_clicked)
b1.on_click(on_button_expand_clicked)
b2.on_click(on_button_download_clicked)
return AppLayout(
left_sidebar=button_list,
center=dis_video,
pane_widths=["50px", 1, 0],
)


try:
from IPython import get_ipython
from IPython.core.interactiveshell import InteractiveShell
Expand All @@ -20,7 +128,9 @@
magics_class,
needs_local_scope,
)
from IPython.display import Image, Video, display
from IPython.display import display
from ipywidgets import AppLayout, Button, GridspecLayout, Layout, Video, widgets

except ImportError:
pass
else:
Expand Down Expand Up @@ -161,20 +271,11 @@ def construct(self):
shutil.copy(local_path, tmpfile)

file_type = mimetypes.guess_type(config["output_file"])[0]
if file_type.startswith("image"):
display(Image(filename=config["output_file"]))
return

# videos need to be embedded when running in google colab
video_embed = "google.colab" in str(get_ipython())

display(
Video(
tmpfile,
html_attributes=f'controls autoplay loop style="max-width: {config["media_width"]};"',
embed=video_embed,
),
)
if file_type.startswith("image"):
display(image_viewer(tmpfile, "350px", "900px"))
jsonvillanueva marked this conversation as resolved.
Show resolved Hide resolved
else:
display(video_viewer(tmpfile, "350px", "900px"))

def add_additional_args(self, args: List[str]) -> List[str]:
additional_args = ["--jupyter"]
Expand Down
3 changes: 1 addition & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,7 @@ isosurfaces = "0.1.0"

[tool.poetry.extras]
webgl_renderer = ["grpcio","grpcio-tools"]
jupyterlab = ["jupyterlab"]
gui = ["dearpygui"]
jupyterlab = ["jupyterlab", "ipywidgets"]

[tool.poetry.dev-dependencies]
pytest-cov = "*"
Expand Down