Skip to content
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

Cannot export to html when matplotlib axis is negative. #127

Closed
apwebber opened this issue Nov 9, 2022 · 8 comments · Fixed by #128
Closed

Cannot export to html when matplotlib axis is negative. #127

apwebber opened this issue Nov 9, 2022 · 8 comments · Fixed by #128
Assignees
Labels
bug Something isn't working

Comments

@apwebber
Copy link
Contributor

apwebber commented Nov 9, 2022

  • esparto version: 4.1.0
  • Python version: 3.9.7
  • Operating System: Windows 10

Description

Cannot export to html when matplotlib axis is negative. I get the error:

UnicodeEncodeError: 'charmap' codec can't encode character '\u2212' in position 13531 character maps to

What I Did

import unittest
import matplotlib.pyplot as plt
import esparto

class TestNegativeChar(unittest.TestCase):
    
    def test_no_negative_char(self):
        
        fig, ax = plt.subplots()
        x = [1,2,3,4,5]
        y = [1,0.5, 0, 1, 2]
        ax.plot(x, y)
        
        page = esparto.Page(title="Test")
        page["Plot"] = fig
        page.save_html("test-fig.html")
    
    def test_negative_char(self):
        """This fails
        
        UnicodeEncodeError: 'charmap' codec can't encode character '\u2212' in position 13531: character maps to <undefined>
        """
        
        fig, ax = plt.subplots()
        x = [1,2,3,4,5]
        y = [-1,-0.5, 0, 1, 2]
        ax.plot(x, y)
        
        page = esparto.Page(title="Test")
        page["Plot"] = fig
        page.save_html("test-fig.html")
@domvwt domvwt self-assigned this Nov 9, 2022
@domvwt domvwt added the bug Something isn't working label Nov 10, 2022
@domvwt
Copy link
Owner

domvwt commented Nov 10, 2022

Thanks for reporting this. I've tried to reproduce on my machine but I was able to run the following code successfully:

import matplotlib.pyplot as plt

import esparto


def test_no_negative_char():

    fig, ax = plt.subplots()
    x = [1,2,3,4,5]
    y = [1,0.5, 0, 1, 2]
    ax.plot(x, y)

    page = esparto.Page(title="Test")
    page["Plot"] = fig
    page.save_html("test-fig-01.html")

def test_negative_char():
    """This fails

    UnicodeEncodeError: 'charmap' codec can't encode character '\u2212' in position 13531: character maps to <undefined>
    """

    fig, ax = plt.subplots()
    x = [1,2,3,4,5]
    y = [-1,-0.5, 0, 1, 2]
    ax.plot(x, y)

    page = esparto.Page(title="Test")
    page["Plot"] = fig
    page.save_html("test-fig-02.html")

if __name__ == "__main__":
    test_no_negative_char()
    test_negative_char()

This makes me think it's a Windows OS specific issue as I've been developing and testing in a Linux environment.

Could you share the full error message so I can pinpoint where the error is occurring?

@domvwt
Copy link
Owner

domvwt commented Nov 10, 2022

One other thing, could you try setting the figure format to PNG as an interim solution?

from esparto import options

options.matplotlib.notebook_format = "png"
options.matplotlib.html_output_format = "png"

@apwebber
Copy link
Contributor Author

Hi domvwt

I have a fix that works on my machine.

In esparto/publish/output.py, line 69

    if filepath:
        Path(filepath).write_text(html_rendered)

Specifying the encoding fixes this for me:

    if filepath:
        Path(filepath).write_text(html_rendered, encoding='utf-8')

This is the full error message I was getting:

File "C:\Users\*\AppData\Local\Programs\Python\Python39\lib\contextlib.py", line 79, in inner
    return func(*args, **kwds)
  File "C:\Users\*\Envs\lab-progress-env\lib\site-packages\esparto\design\layout.py", line 482, in save_html
    html = publish_html(
  File "C:\Users\*\Envs\lab-progress-env\lib\site-packages\esparto\publish\output.py", line 70, in publish_html
    Path(filepath).write_text(html_rendered)
  File "C:\Users\*\AppData\Local\Programs\Python\Python39\lib\pathlib.py", line 1286, in write_text
    return f.write(data)
  File "C:\Users\*\AppData\Local\Programs\Python\Python39\lib\encodings\cp1252.py", line 19, in encode
    return codecs.charmap_encode(input,self.errors,encoding_table)[0]
UnicodeEncodeError: 'charmap' codec can't encode character '\u2212' in position 13531: character maps to <undefined>

@apwebber
Copy link
Contributor Author

apwebber commented Nov 10, 2022

Although a funny side effect of the above fix is that the page icon is now random letters:

<link href="data:image/svg+xml,<svg xmlns=%22http://www.w3.org/2000/svg%22 viewBox=%220 0 100 100%22><text y=%22.9em%22 font-size=%2290%22>📊</text></svg>" rel="icon">

@domvwt
Copy link
Owner

domvwt commented Nov 10, 2022

What if we replace the text with this &#128202;?

This is on line 7 of https://github.com/domvwt/esparto/blob/main/esparto/resources/jinja/base.html.jinja.

@apwebber
Copy link
Contributor Author

Yes that works :)

@domvwt
Copy link
Owner

domvwt commented Nov 10, 2022

Great, thanks for making the fix!

If you can make a pull request with your changes I'll merge and make a new release.

@domvwt
Copy link
Owner

domvwt commented Nov 10, 2022

I've made a develop branch that you can use as the base - I think you'll need to fork the repository first if you haven't already.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants