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

Windows下render可以指定CRLF为LF么? #2325

Closed
nyssance opened this issue May 13, 2024 · 11 comments
Closed

Windows下render可以指定CRLF为LF么? #2325

nyssance opened this issue May 13, 2024 · 11 comments
Assignees
Labels
feature Proposal discuss proposal

Comments

@nyssance
Copy link

问题

Windows下render默认是CRLF,没有找到设置newline="\n"的地方

运行环境(系统环境及 pyecharts 版本)

Windows 11 / pyecharts 2.0.5

代码及截图

@sunhailin-Leo
Copy link
Member

@nyssance

  • 基于什么场景会存在这个问题?

@sunhailin-Leo sunhailin-Leo added the hope more description hope developer can give more description of issue label May 14, 2024
@nyssance
Copy link
Author

nyssance commented May 15, 2024

@nyssance

  • 基于什么场景会存在这个问题?

双系统用户,生成的文件需要保存,mac下生成的文件是LF,windows下生成的是CRLF,内容没改动,但是git就不一样了。一般的写文件,输出前可以自己把output处理一遍再输出,但pyecharts的render是封装了。所以没法自己前置操作,要改成LF还要去生成后遍历已经生成的文件改。pandas/jinja会留个配置或者参数让用户自己改一下。

@sunhailin-Leo
Copy link
Member

@nyssance

  • 基于什么场景会存在这个问题?

双系统用户,生成的文件需要保存,mac下生成的文件是LF,windows下生成的是CRLF,内容没改动,但是git就不一样了。一般的写文件,输出前可以自己把output处理一遍再输出,但pyecharts的render是封装了。所以没法自己前置操作,要改成LF还要去生成后遍历已经生成的文件改。pandas/jinja会留个配置或者参数让用户自己改一下。

  • git 也是必须统一用一种的,切换系统势必会影响到格式。
  • 你说 jinja 留的配置指的是这个嘛?https://jinja.palletsprojects.com/en/3.1.x/api/#jinja2.Environment
    • 如果你说的是 jinja2.Environment 的话,目前 pyecharts 所有 render api 都有一个 env 参数用来传递 jinja2.Environment 的配置哈~

@nyssance
Copy link
Author

nyssance commented May 16, 2024

@nyssance

  • 基于什么场景会存在这个问题?

双系统用户,生成的文件需要保存,mac下生成的文件是LF,windows下生成的是CRLF,内容没改动,但是git就不一样了。一般的写文件,输出前可以自己把output处理一遍再输出,但pyecharts的render是封装了。所以没法自己前置操作,要改成LF还要去生成后遍历已经生成的文件改。pandas/jinja会留个配置或者参数让用户自己改一下。

  • git 也是必须统一用一种的,切换系统势必会影响到格式。

  • 你说 jinja 留的配置指的是这个嘛?https://jinja.palletsprojects.com/en/3.1.x/api/#jinja2.Environment

    • 如果你说的是 jinja2.Environment 的话,目前 pyecharts 所有 render api 都有一个 env 参数用来传递 jinja2.Environment 的配置哈~

是的我git用的是统一的,jinja它是这样的

template = env.get_template(template_name)
output = template.render(data=data)

jinja的env里面它不管LF还是CRLF,它只能控制utf8(这个也影响双平台,windows下默认不是utf8),但是它的output写入文件的时候,我用pathlib的Pathpath.write_text(output, newline="\n"),python给我一个机会去自己处理。
pyecharts的语法如果能类似 chart.render(f"../{html}", newline="\n"),也这样留个参数就能处理了。目前就是同样代码mac和windows下用生成的行尾不同。
pandas是这么处理的df.to_csv(path, index=False, lineterminator="\n"),把数据写入csv文件,直接提供一个参数,来保证双平台输出的统一。

@sunhailin-Leo
Copy link
Member

@nyssance

  • 这个需求比较罕见,我们得考虑一下~
  • 另外,按你的说法,如果要在项目兼容 mac/linux 和 windows 的话,是不是得输出两种换行模式的文件?这样反而会让开发者感到疑惑吧?
  • 如果在多人协作中用 git 模式来开发或者编辑器统一换行模式,这种问题应该不会存在的。

@nyssance
Copy link
Author

nyssance commented May 18, 2024

@nyssance

  • 这个需求比较罕见,我们得考虑一下~
  • 另外,按你的说法,如果要在项目兼容 mac/linux 和 windows 的话,是不是得输出两种换行模式的文件?这样反而会让开发者感到疑惑吧?
  • 如果在多人协作中用 git 模式来开发或者编辑器统一换行模式,这种问题应该不会存在的。

有了参数才能保证输出一种,自己在代码里指定是\n还是\r\n。现在不处理,同样的代码在mac和windows下运行,会render出不同的文件,是两种。这也是为什么pandas和python官方的write_text都会留一个参数的原因。

@nyssance
Copy link
Author

nyssance commented May 18, 2024

具体来说,因为pyecharts已经是python3.6+了,所以把engine.py里的这段

def write_utf8_html_file(file_name: str, html_content: str):
    with open(file_name, "w+", encoding="utf-8") as html_file:
        html_file.write(html_content)

改成

from pathlib import Path

def write_utf8_html_file(file_name: str, html_content: str, newline: str | None = None):
    Path(file_name).write_text(html_content, encoding="utf-8", newline=newline)

然后把newline向上一层层都留出来就可以。用户不填,保持现在的默认行为,mac/windows不同行尾。用户填了,就指定。

@sunhailin-Leo
Copy link
Member

具体来说,因为pyecharts已经是python3.6+了,所以把engine.py里的这段

def write_utf8_html_file(file_name: str, html_content: str):
    with open(file_name, "w+", encoding="utf-8") as html_file:
        html_file.write(html_content)

改成

from pathlib import Path

def write_utf8_html_file(file_name: str, html_content: str, newline: str | None = None):
    Path(file_name).write_text(html_content, encoding="utf-8", newline=newline)

然后把newline向上一层层都留出来就可以。用户不填,保持现在的默认行为,mac/windows不同行尾。用户填了,就指定。

@sunhailin-Leo sunhailin-Leo added Proposal discuss proposal and removed hope more description hope developer can give more description of issue labels May 20, 2024
@sunhailin-Leo sunhailin-Leo reopened this May 20, 2024
@nyssance
Copy link
Author

nyssance commented May 20, 2024

那就算了,为了这个极小众需求特地再去用复杂方式处理不值得,等哪天只支持3.10+再解决就行了。

@sunhailin-Leo
Copy link
Member

@nyssance

  • 你的这个需求有个办法可以解决了。
  • 大概构造像这样子。open(file_name, "w+", encoding="utf-8", newline=os.linesep) 然后我这边做一个全局变量,类似 CurrentConfig 的设置,默认取值按照 os.linesep 来就可以了~

@sunhailin-Leo
Copy link
Member

@nyssance

  • Closed it then waiting for the new version.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature Proposal discuss proposal
Projects
None yet
Development

No branches or pull requests

2 participants