Skip to content
Merged
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
32 changes: 19 additions & 13 deletions opencodeblocks/graphics/blocks/codeblock.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,16 @@
""" Module for the base OCB Code Block. """


import re

from PyQt5.QtCore import QCoreApplication, QByteArray
from PyQt5.QtGui import QPixmap
from PyQt5.QtWidgets import QLabel, QPushButton
from PyQt5.QtWidgets import QPushButton, QTextEdit

from ansi2html import Ansi2HTMLConverter

from opencodeblocks.graphics.blocks.block import OCBBlock
from opencodeblocks.graphics.pyeditor import PythonEditor

ansi_escape = re.compile(r'\x1B(?:[@-Z\\-_]|\[[0-?]*[ -/]*[@-~])')
conv = Ansi2HTMLConverter()


class OCBCodeBlock(OCBBlock):
Expand Down Expand Up @@ -88,10 +87,14 @@ def stdout(self, value: str):
# text output
self.image = ""

# Remove ANSI color codes
text = ansi_escape.sub('', value)
# Remove backspaces (tf loading bars)
text = text.replace('\x08', '')
# Remove ANSI backspaces
text = value.replace("\x08", "")
# Convert ANSI escape codes to HTML
text = conv.convert(text)
# Replace background color
text = text.replace('background-color: #000000',
'background-color: #434343')

self.output_panel.setText(text)

@property
Expand All @@ -105,11 +108,12 @@ def image(self, value: str):
if hasattr(self, 'source_editor') and self.image != "":
# If there is an image output, erase the text output and display
# the image output
self.output_panel.setText("")
text = ""
ba = QByteArray.fromBase64(str.encode(self.image))
pixmap = QPixmap()
pixmap.loadFromData(ba)
self.output_panel.setPixmap(pixmap)
text = '<img src="data:image/png;base64,{}">'.format(self.image)
self.output_panel.setText(text)

@source.setter
def source(self, value: str):
Expand All @@ -120,9 +124,11 @@ def source(self, value: str):

def init_output_panel(self):
""" Initialize the output display widget: QLabel """
output_panel = QLabel()
output_panel.setText("")

output_panel = QTextEdit()
output_panel.setReadOnly(True)
output_panel.setStyleSheet(
"QTextEdit { background-color: #434343; }"
)
self.splitter.addWidget(output_panel)
return output_panel

Expand Down
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ QtPy>=1.9.0
qscintilla>=2.13.0
Ipython>=7.27.0
jupyter_client>=7.0.6
ipykernel>=6.5.0
ipykernel>=6.5.0
ansi2html>=1.6.0