Last active
February 2, 2024 21:39
-
-
Save n0kovo/d27e9b724a8f3eedb35c53783a56c525 to your computer and use it in GitHub Desktop.
quick text formatter for Colab
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import subprocess | |
from IPython.display import display, HTML | |
class StatusRunner: | |
def __init__(self, success_message="Command executed successfully, no output.", error_critical=True): | |
self.success_message = success_message | |
self.error_critical = error_critical | |
def __enter__(self): | |
# The enter method is called when entering the context | |
return self.run | |
def __exit__(self, exc_type, exc_value, traceback): | |
# The exit method is called when exiting the context | |
# You can handle exceptions here if needed | |
pass | |
def format_html_message(self, message): | |
"""Format the message for HTML display.""" | |
return message.replace('\n', '<br>').replace(' ', ' ') | |
def display_status(self, title, message, title_color, is_critical=False, emoji=""): | |
"""Display status message with specified color and size.""" | |
size = 'h2' if is_critical else 'h3' | |
message_html = self.format_html_message(message) | |
title_html = f"<{size} style='color: {title_color};'>{emoji} {title}</{size}><br>" | |
display(HTML(title_html + message_html)) | |
def run(self, command): | |
print(f"Running '{command}' ...") | |
try: | |
result = subprocess.run(command, capture_output=True, text=True, check=True, shell=True) | |
message = result.stdout.strip() if result.stdout.strip() else self.success_message | |
self.display_status(title="Success!", message=message, title_color='green', emoji="✅") | |
except subprocess.CalledProcessError as e: | |
error_message = f"Fix before proceeding!<br>See below error message:<br><br>{e.stderr}" | |
self.display_status(title="Error!", message=error_message, title_color='red', is_critical=self.error_critical, emoji="🚨") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment