Skip to content

Refactor view methods in file handlers to accept width and height parameters #1862

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
10 changes: 5 additions & 5 deletions edsl/scenarios/file_methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,18 +71,18 @@ def supported_file_types(cls):
return list(cls._handlers.keys())

@abstractmethod
def view_system(self):
def view_system(self, width: int = None, height: int = None):
...

@abstractmethod
def view_notebook(self):
def view_notebook(self, width: int = None, height: int = None):
...

def view(self):
def view(self, width: int = None, height: int = None):
if is_notebook():
self.view_notebook()
self.view_notebook(width=width, height=height)
else:
self.view_system()
self.view_system(width=width, height=height)

@abstractmethod
def example(self):
Expand Down
4 changes: 2 additions & 2 deletions edsl/scenarios/file_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -459,10 +459,10 @@ def to_tempfile(self, suffix=None):

return temp_file.name

def view(self) -> None:
def view(self, width: int = None, height: int = None) -> None:
handler = FileMethods.get_handler(self.suffix)
if handler:
handler(self.path).view()
handler(self.path).view(width=width, height=height)
else:
print(f"Viewing of {self.suffix} files is not supported.")

Expand Down
4 changes: 2 additions & 2 deletions edsl/scenarios/handlers/csv_file_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
class CsvMethods(FileMethods):
suffix = "csv"

def view_system(self):
def view_system(self, width: int = None, height: int = None):
import os
import subprocess

Expand All @@ -21,7 +21,7 @@ def view_system(self):
else:
print("CSV file was not found.")

def view_notebook(self):
def view_notebook(self, width: int = None, height: int = None):
import pandas as pd
from IPython.display import display

Expand Down
9 changes: 6 additions & 3 deletions edsl/scenarios/handlers/docx_file_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def extract_text(self):
text = "\n".join(full_text)
return text

def view_system(self):
def view_system(self, width: int = None, height: int = None):
import os
import subprocess

Expand All @@ -36,18 +36,21 @@ def view_system(self):
else:
print("DOCX file was not found.")

def view_notebook(self):
def view_notebook(self, width: int = None, height: int = None):
try:
import mammoth
except ImportError:
print("mammoth is not installed. Please install it using 'pip install mammoth'.")
return
from IPython.display import HTML, display

_width = width if width is not None else 800
_height = height if height is not None else 800

with open(self.path, "rb") as docx_file:
result = mammoth.convert_to_html(docx_file)
html = f"""
<div style="width: 800px; height: 800px; padding: 20px;
<div style="width: {_width}px; height: {_height}px; padding: 20px;
border: 1px solid #ccc; overflow-y: auto;">
{result.value}
</div>
Expand Down
8 changes: 5 additions & 3 deletions edsl/scenarios/handlers/html_file_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
class HtmlMethods(FileMethods):
suffix = "html"

def view_system(self):
def view_system(self, width: int = None, height: int = None):
import webbrowser

# with open(self.path, "r") as f:
Expand All @@ -14,10 +14,12 @@ def view_system(self):
# webbrowser.open("file://" + html_path)
webbrowser.open("file://" + self.path)

def view_notebook(self):
def view_notebook(self, width: int = None, height: int = None):
from IPython.display import IFrame, display

display(IFrame(self.path, width=800, height=800))
_width = width if width is not None else 800
_height = height if height is not None else 800
display(IFrame(self.path, width=_width, height=_height))

def example(self):
html_string = b"""
Expand Down
6 changes: 3 additions & 3 deletions edsl/scenarios/handlers/jpeg_file_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
class JpegMethods(FileMethods):
suffix = "jpeg"

def view_system(self):
def view_system(self, width: int = None, height: int = None):
import os
import subprocess

Expand All @@ -22,10 +22,10 @@ def view_system(self):
else:
print("JPEG file was not found.")

def view_notebook(self):
def view_notebook(self, width: int = None, height: int = None):
from IPython.display import Image, display

display(Image(filename=self.path))
display(Image(filename=self.path, width=width, height=height))

def example(self):
import matplotlib.pyplot as plt
Expand Down
4 changes: 2 additions & 2 deletions edsl/scenarios/handlers/json_file_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
class JsonMethods(FileMethods):
suffix = "json"

def view_system(self):
def view_system(self, width: int = None, height: int = None):
import os
import subprocess

Expand All @@ -24,7 +24,7 @@ def view_system(self):
else:
print("JSON file was not found.")

def view_notebook(self):
def view_notebook(self, width: int = None, height: int = None):
from IPython.display import FileLink, JSON, display
import json

Expand Down
4 changes: 2 additions & 2 deletions edsl/scenarios/handlers/md_file_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
class MarkdownMethods(FileMethods):
suffix = "md"

def view_system(self):
def view_system(self, width: int = None, height: int = None):
import os
import subprocess

Expand All @@ -22,7 +22,7 @@ def view_system(self):
else:
print("Markdown file was not found.")

def view_notebook(self):
def view_notebook(self, width: int = None, height: int = None):
from IPython.display import FileLink, Markdown, display

# First display the content of the markdown file
Expand Down
9 changes: 6 additions & 3 deletions edsl/scenarios/handlers/mp4_file_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class Mp4Methods(FileMethods):
"""
suffix = "mp4"

def view_system(self):
def view_system(self, width: int = None, height: int = None):
"""
Open the MP4 file with the system's default video player.
"""
Expand All @@ -31,13 +31,16 @@ def view_system(self):
else:
print("MP4 file was not found.")

def view_notebook(self):
def view_notebook(self, width: int = None, height: int = None):
"""
Display the MP4 video in a Jupyter notebook using IPython's HTML display.
"""
from IPython.display import HTML, display
import base64

_width = width if width is not None else 640
_height = height if height is not None else 360

# Read the video file and encode it as base64
with open(self.path, 'rb') as f:
video_data = f.read()
Expand All @@ -46,7 +49,7 @@ def view_notebook(self):

# Create an HTML5 video element with the base64-encoded video
video_html = f"""
<video width="640" height="360" controls>
<video width="{_width}" height="{_height}" controls>
<source src="data:video/mp4;base64,{video_base64}" type="video/mp4">
Your browser does not support the video tag.
</video>
Expand Down
11 changes: 7 additions & 4 deletions edsl/scenarios/handlers/pdf_file_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def extract_text(self):

return text

def view_system(self):
def view_system(self, width: int = None, height: int = None):
import subprocess

if os.path.exists(self.path):
Expand All @@ -42,17 +42,20 @@ def view_system(self):
else:
print("PDF file was not found.")

def view_notebook(self):
def view_notebook(self, width: int = None, height: int = None):
from IPython.display import HTML, display

_width = width if width is not None else 800
_height = height if height is not None else 800

with open(self.path, "rb") as f:
base64_pdf = base64.b64encode(f.read()).decode("utf-8")

html = f"""
<iframe
src="data:application/pdf;base64,{base64_pdf}"
width="800px"
height="800px"
width="{_width}px"
height="{_height}px"
type="application/pdf"
></iframe>
"""
Expand Down
6 changes: 3 additions & 3 deletions edsl/scenarios/handlers/png_file_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
class PngMethods(FileMethods):
suffix = "png"

def view_system(self):
def view_system(self, width: int = None, height: int = None):
import os
import subprocess

Expand All @@ -22,10 +22,10 @@ def view_system(self):
else:
print("PNG file was not found.")

def view_notebook(self):
def view_notebook(self, width: int = None, height: int = None):
from IPython.display import Image, display

display(Image(filename=self.path))
display(Image(filename=self.path, width=width, height=height))

def example(self):
import matplotlib.pyplot as plt
Expand Down
9 changes: 6 additions & 3 deletions edsl/scenarios/handlers/pptx_file_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def extract_text(self):
text = "\n\n".join(full_text)
return text

def view_system(self):
def view_system(self, width: int = None, height: int = None):
import os
import subprocess

Expand All @@ -40,10 +40,13 @@ def view_system(self):
else:
print("PPTX file was not found.")

def view_notebook(self):
def view_notebook(self, width: int = None, height: int = None):
from pptx import Presentation
from IPython.display import HTML, display

_width = width if width is not None else 800
_height = height if height is not None else 800

prs = Presentation(self.path)

# Create a simple HTML representation of the slides
Expand All @@ -64,7 +67,7 @@ def view_notebook(self):
)

html = f"""
<div style="width: 800px; height: 800px; padding: 20px;
<div style="width: {_width}px; height: {_height}px; padding: 20px;
border: 1px solid #ccc; overflow-y: auto;">
{''.join(html_content)}
</div>
Expand Down
4 changes: 2 additions & 2 deletions edsl/scenarios/handlers/py_file_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
class PyMethods(FileMethods):
suffix = "py"

def view_system(self):
def view_system(self, width: int = None, height: int = None):
"""Open the Python file in the system's default editor."""
import os
import subprocess
Expand All @@ -29,7 +29,7 @@ def view_system(self):
else:
print("Python file was not found.")

def view_notebook(self):
def view_notebook(self, width: int = None, height: int = None):
"""Display the Python file with syntax highlighting in a notebook."""
from IPython.display import FileLink, display, HTML
import pygments
Expand Down
4 changes: 2 additions & 2 deletions edsl/scenarios/handlers/sql_file_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
class SqlMethods(FileMethods):
suffix = "sql"

def view_system(self):
def view_system(self, width: int = None, height: int = None):
import os
import subprocess

Expand All @@ -26,7 +26,7 @@ def view_system(self):
else:
print("SQL file was not found.")

def view_notebook(self):
def view_notebook(self, width: int = None, height: int = None):
from IPython.display import FileLink, display, HTML
import pygments
from pygments.lexers import SqlLexer
Expand Down
10 changes: 7 additions & 3 deletions edsl/scenarios/handlers/sqlite_file_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def extract_text(self):

return "\n".join(full_text)

def view_system(self):
def view_system(self, width: int = None, height: int = None):
"""
Opens the database with the system's default SQLite viewer if available.
"""
Expand All @@ -70,12 +70,16 @@ def view_system(self):
else:
print("SQLite database file was not found.")

def view_notebook(self):
def view_notebook(self, width: int = None, height: int = None):
"""
Displays database contents in a Jupyter notebook.
"""
import pandas as pd
from IPython.display import HTML, display
import sqlite3

_width = width if width is not None else 800
_height = height if height is not None else 800

with sqlite3.connect(self.path) as conn:
# Get all table names
Expand All @@ -99,7 +103,7 @@ def view_notebook(self):

# Combine all tables into one scrollable div
html = f"""
<div style="width: 800px; height: 800px; padding: 20px;
<div style="width: {_width}px; height: {_height}px; padding: 20px;
border: 1px solid #ccc; overflow-y: auto;">
{''.join(html_parts)}
</div>
Expand Down
4 changes: 2 additions & 2 deletions edsl/scenarios/handlers/txt_file_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
class TxtMethods(FileMethods):
suffix = "txt"

def view_system(self):
def view_system(self, width: int = None, height: int = None):
import os
import subprocess

Expand All @@ -22,7 +22,7 @@ def view_system(self):
else:
print("TXT file was not found.")

def view_notebook(self):
def view_notebook(self, width: int = None, height: int = None):
from ...display import FileLink, display

display(FileLink(self.path))
Expand Down
Loading