MCPcopy Create free account
hub / github.com/Derive-Risk/pyfair / base64ify

Method base64ify

pyfair/report/base_report.py:117–160  ·  view source on GitHub ↗

Binary data into embeddable tag with base64 data To avoid having separate image files, pyfair simply embeds report images as base64 image tags. base64ify() is a convenience function that creates these tags. image : [bytes, str, pathlib.Path]

(self, image, alternative_text='', options='')

Source from the content-addressed store, hash-verified

115 return self._format_strings
116
117 def base64ify(self, image, alternative_text='', options=''):
118 """Binary data into embeddable <img> tag with base64 data
119
120 To avoid having separate image files, pyfair simply embeds report
121 images as base64 image tags. base64ify() is a convenience function
122 that creates these tags.
123
124 image : [bytes, str, pathlib.Path]
125 The binary data, path string, or pathlib.Path containing either
126 the data itself or a file of data.
127
128 alternative_text: str, optional
129 Alternative text to be showed in the event the image does not
130 properly render
131
132 options : str, optional
133 A string containing additional HTML attributes to be placed
134 between "<image " and " src="
135
136 Returns
137 -------
138 str
139 An HTML <img> tag with base64 data and appropriate attributes
140
141 Raises
142 ------
143 TypeError
144 If the image parameter supplied is of an inappropriate type
145
146 """
147 # If path, open and read.
148 if type(image) == str or isinstance(image, pathlib.Path):
149 with open(image, 'rb') as f:
150 binary_data = f.read()
151 # If bytes, jsut write
152 elif type(image) == bytes:
153 binary_data = image
154 else:
155 raise TypeError(str(image) + ' is not a string, path, or bytes.')
156 # Get base64 string
157 base64_string = base64.b64encode(binary_data).decode('utf8')
158 # Create tag
159 tag = f'<img {options} src="data:image/png;base64, {base64_string}" alt="{alternative_text}"/>'
160 return tag
161
162 def _construct_output(self):
163 """Stub to be overridden by subclass to generate report"""

Callers 3

_fig_to_img_tagMethod · 0.95
test_base64ifyMethod · 0.80
_construct_outputMethod · 0.80

Calls

no outgoing calls

Tested by 1

test_base64ifyMethod · 0.64