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

Convert large json file to html raises "MemoryError" #372

Closed
vincentping opened this issue Sep 24, 2014 · 4 comments
Closed

Convert large json file to html raises "MemoryError" #372

vincentping opened this issue Sep 24, 2014 · 4 comments

Comments

@vincentping
Copy link

I'm using jinja 2.7.3 to convert test log (json file) to html, the template using is pretty simple:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<head>
    <meta charset="UTF-8" />
 <title></title>
</head>
<body>
{%- for item in  logs -%}
<table>
    <tr>
    <td width="80"><nobr>APDU:</nobr></td>
    <td>{{ item['apdu'] }}</td>
    </tr>
    <tr>
    <td width="80"><nobr>Response:</nobr></td>
    <td> {{ item['aresp'] }}</td>
    </tr>
    <tr>
    <td width="80"><nobr>ExpectData:</nobr></td>
    <td>{{ item.get('expdata','None') }} </td>
    </tr>
    <tr>
    <td width="80"><nobr>ExpectStatus:</nobr></td>
    <td>{{ item.get('expsw','None') }}  </td>
    </tr>
    <tr>
    <td width="80"><nobr>ErrorInfo:</nobr></td>
    <td>{% if 'errorinfo' in item -%}<font color='red'>Error!</font> {{ item['errorinfo'] }}{%- else %}None{%- endif%}</td>
    </tr>
</table>
<hr>
{% endfor %}
</body>
</html>

I use a simple script to convert it:

import json
import jinja2

template_dir =r'E:\test'
env=jinja2.Environment(loader=jinja2.FileSystemLoader(template_dir))

logfile = r'.\a.json'
with open(logfile, 'r') as fi:
    data = json.load(fi)

template = env.get_template('readlog.htm')
templateVars = {"logs": data["logitem"]}

with open('b.html','w') as fo:
    fo.write(template.render(templateVars))

When the log file is large near 30M, the template.render(templateVars) raise "MemoryError", I tried some other log files smaller, such 2-10M, the script runs fine.

Traceback (most recent call last):
  File "test.py", line 15, in <module>
    fo.write(template.render(templateVars))
  File "D:\python27\lib\site-packages\jinja2\environment.py", line 969, in render
    return self.environment.handle_exception(exc_info, True)
  File "D:\python27\lib\site-packages\jinja2\environment.py", line 736, in handle_exception
    traceback = _make_traceback(exc_info, source_hint)
  File "D:\python27\lib\site-packages\jinja2\debug.py", line 140, in make_traceback
    return translate_exception(exc_info, initial_skip)
  File "D:\python27\lib\site-packages\jinja2\debug.py", line 193, in translate_exception
    reraise(exc_info[0], exc_info[1], exc_info[2])
  File "D:\python27\lib\site-packages\jinja2\environment.py", line 966, in render
    return concat(self.root_render_func(self.new_context(vars)))
MemoryError

My environnement is:
Windows XP
Python 2.7.8
Jinja2 : 2.7.3

@vincentping vincentping changed the title Large json file converts to html raise "MemoryError" Converts large json file to html raise "MemoryError" Sep 24, 2014
@vincentping vincentping changed the title Converts large json file to html raise "MemoryError" Convert large json file to html raise "MemoryError" Sep 24, 2014
@vincentping vincentping changed the title Convert large json file to html raise "MemoryError" Convert large json file to html raises "MemoryError" Sep 24, 2014
@manhg
Copy link

manhg commented Sep 24, 2014

I think you can work around this, eg by using generator instead of passing a big templateVars.
And instead of looping in the template, you can use Jinja for only HTML inside tag; do loop in Python. Output to file right after rendering (chunk by chunk); it will help you avoid memory overflow.

@vincentping
Copy link
Author

@manhg Thank you very much, it works!

@manhg
Copy link

manhg commented Sep 24, 2014

: ) I'm glad to hear it works.

@mitsuhiko
Copy link
Contributor

Also for future reference: Jinja can iteratively render templates with the stream system.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 13, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants