-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
12 changed files
with
322 additions
and
123 deletions.
There are no files selected for viewing
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# | ||
|
||
""" | ||
This file defines the `HtmlUtils` classes and utility functions | ||
""" | ||
|
||
from .utils import system_time_millis | ||
|
||
|
||
class HtmlUtils: | ||
""" Utility class for formatting code as HTML and other notebook related formatting | ||
""" | ||
|
||
def __init__(self): | ||
pass | ||
|
||
@classmethod | ||
def formatCodeAsHtml(cls, codeText): | ||
""" Formats supplied code as Html suitable for use with notebook ``displayHTML`` | ||
:param codeText: Code to be wrapped in html section | ||
:return: Html string | ||
This will wrap the code with a html section using html ``pre`` and ``code`` tags. | ||
It adds a copy text to clipboard button to enable users to easily copy the code to the clipboard. | ||
It does not reformat code so supplied code should be preformatted into lines. | ||
.. note:: | ||
As the notebook environment uses IFrames in rendering html within ``displayHtml``, it cannot use | ||
the newer ``navigator`` based functionality as this is blocked for cross domain IFrames by default. | ||
""" | ||
ts = system_time_millis() | ||
|
||
formattedCode = f""" | ||
<h3>Generated Code</h3> | ||
<div style="outline: 1px dashed blue;"><p ><pre><code id="generated_code_{ts}"> | ||
{codeText} | ||
</code></pre></p></br> | ||
</div> | ||
<p><button type="button" onclick="dbldatagen_copy_code_to_clipboard()">Copy code to clipboard!</button> </p> | ||
<script> | ||
function dbldatagen_copy_code_to_clipboard() {{ | ||
try {{ | ||
var r = document.createRange(); | ||
r.selectNode(document.getElementById("generated_code_{ts}")); | ||
window.getSelection().removeAllRanges(); | ||
window.getSelection().addRange(r); | ||
document.execCommand('copy'); | ||
window.getSelection().removeAllRanges(); | ||
}} | ||
catch {{ | ||
console.error("copy to clipboard failed") | ||
}} | ||
}} | ||
</script> | ||
""" | ||
|
||
return formattedCode | ||
|
||
@classmethod | ||
def formatTextAsHtml(cls, textContent, title="Output"): | ||
""" Formats supplied text as Html suitable for use with notebook ``displayHTML`` | ||
:param textContent: Text to be wrapped in html section | ||
:param title: Title text to be used | ||
:return: Html string | ||
This will wrap the text content with with Html formatting | ||
""" | ||
ts = system_time_millis() | ||
formattedContent = f""" | ||
<h3>{title}</h3> | ||
<div style="outline: 1px dashed blue;"><p ><pre id="generated_content_{ts}"> | ||
{textContent} | ||
</pre></p></br> | ||
</div> | ||
<p><button type="button" onclick="dbldatagen_copy_to_clipboard()">Copy output to clipboard!</button></p> | ||
<script> | ||
function dbldatagen_copy_to_clipboard() {{ | ||
try {{ | ||
var r = document.createRange(); | ||
r.selectNode(document.getElementById("generated_content_{ts}")); | ||
window.getSelection().removeAllRanges(); | ||
window.getSelection().addRange(r); | ||
document.execCommand('copy'); | ||
window.getSelection().removeAllRanges(); | ||
}} | ||
catch {{ | ||
console.error("copy to clipboard failed") | ||
}} | ||
}} | ||
</script> | ||
""" | ||
|
||
return formattedContent |
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import pytest | ||
|
||
from dbldatagen import HtmlUtils, SparkSingleton | ||
|
||
spark = SparkSingleton.getLocalInstance("unit tests") | ||
|
||
|
||
class TestHtmlUtils: | ||
|
||
@pytest.mark.parametrize("content", | ||
[""" | ||
for x in range(10): | ||
print(x) | ||
"""] | ||
) | ||
def test_html_format_code(self, content): | ||
formattedContent = HtmlUtils.formatCodeAsHtml(content) | ||
assert formattedContent is not None | ||
assert content in formattedContent | ||
|
||
@pytest.mark.parametrize("content, heading", | ||
[(""" | ||
this is a test | ||
this is another one | ||
""", "testing" | ||
)]) | ||
def test_html_format_content(self, content, heading): | ||
formattedContent = HtmlUtils.formatTextAsHtml(content, title=heading) | ||
|
||
assert formattedContent is not None, "formatted output is None" | ||
|
||
assert content in formattedContent, "original content missing" | ||
assert heading in formattedContent, "heading missing from content" |
Oops, something went wrong.