Comments (7)
I can send correctly using bytes - resp.write(b'абвгд')
Yes, that's the correct way to do it - .write() accepts bytes
object. MicroPython is more relaxed than CPython and allows to pass str
in many places, but that may not work reliably for non-ASCII chars, as you noticed.
The change you made to render_template() looks plausible, but I would like to consider any other possible alternatives. Can you provide small self-contained example which reproduces the problem to speed up the resolution?
from picoweb.
Here is the script:
import picoweb
app = picoweb.WebApp(__name__)
def render_template_b(writer, template_name, args=()):
import utemplate.source
template_loader = utemplate.source.Loader("__main__", "templates")
template = template_loader.load(template_name)
for s in template(*args):
yield from writer.awrite(s.encode('utf-8'))
@app.route("/ascii")
def index(req, resp):
yield from picoweb.start_response(resp, content_type = "text/html")
yield from app.render_template(resp, "ascii.tpl", ("some ascii chars",))
@app.route("/non-ascii-fix")
def test(req, resp):
yield from picoweb.start_response(resp, content_type = "text/html; charset=utf-8")
yield from render_template_b(resp, "non-ascii.tpl", ("абвгдe",))
@app.route("/non-ascii-error")
def test2(req, resp):
yield from picoweb.start_response(resp, content_type = "text/html; charset=utf-8")
yield from app.render_template(resp, "non-ascii.tpl", ("абвгдe",))
import ulogging as logging
logging.basicConfig(level=logging.INFO)
app.run(debug=True)
ascii.tpl
:
{% args chars %}
<html>
ascii chars: abcde <br>
From args: {{ chars }} <br>
</html>
non-ascii.tpl
:
{% args chars %}
<html>
Non ascii chars: абвгде<br>
From args: {{ chars }} <br>
</html>
from picoweb.
Ok, so my thoughts about this stuff:
- Ideally, I'd like .awrite() to just work with
str
. - However, I'm a bit shy to add
if isinstance(buf, str): buf = buf.encode()
to it, conceptually it's "wrong" to make "well-behaving" apps, which shove thebytes
in, to incur overhead of that check. - Then patching it on the level of render_template(), like proposed here would be the way, right?
- No, wait there's p.1.
- Looking at StreamWriter.awrite() again, to see if it can be "optimized", in a way that bytes and str would be handled transparently. Well, it's already optimized, and that optimization depends on
bytes
. - But well, then there's an option to introduce .awritestr(), and put conversion there. This way, both uasyncio and picoweb need to be patched now, but at least it's a scalable solution reusable for other cases.
from picoweb.
An interesting solution would be to have (a null) .encode() method on bytes
, and call buf = buf.encode()
unconditionally in .awrite(). That's actually how MicroPython works if built without MICROPY_PY_BUILTINS_STR_UNICODE defined. But otherwise, I saw (ugly, CPython-ridden) code which uses presence of encode/decode methods for ducktyping checks, and that's why with MICROPY_PY_BUILTINS_STR_UNICODE, there're separate method tables, even if they differ only in encode vs decode.
from picoweb.
So, overall, .awritestr() seems like the best solution.
Comments and criticism are welcome though.
from picoweb.
Right. I just want to say that I am not so experience in python yet. Now I am working on project which will use this fix. I will try to imlement it and share results.
from picoweb.
Right. I just want to say that I am not so experience in python yet.
Yes, sometimes it's not easy to decide which is the best way to fix some issue. Anyway, I went for the solution described above and implemented it in picoweb 1.6. I've also added example to test Unicode rendering functionality: https://github.com/pfalcon/picoweb/blob/master/examples/example_unicode.py .
Hopefully, this fixes issues on your side too. If not, feel free to report them.
from picoweb.
Related Issues (20)
- Incorrect uasyncio function call awitestr() and memory leak issues as resolved in comments. HOT 7
- Some things I miss... HOT 4
- TypeError: function expected at most 3 arguments, got 4 HOT 1
- Headers are stored unaltered, making them case sensitive. HTTP spec says they should be insensitive HOT 1
- Exception on ESP8266 HOT 3
- Running Picoweb on micropython HOT 3
- Picoweb is eating memory: How to debug? HOT 1
- Larger number of parallel connections fail HOT 2
- Restarting server
- How do I install picoweb? HOT 2
- Is it possible to implement a 404 Error handler? HOT 2
- installation failure on ESP-12F (ESP8266) HOT 1
- app.render_template: OSError: [Errno 2] ENOENT (esp8266) HOT 1
- error while using picoweb.WebApp() HOT 1
- favicon.ico
- Getting file from form data
- TypeError("'NoneType' object isn't iterable",) HOT 1
- How to get client ip and port? HOT 1
- example_webapp.py does't work on fresh upip install of picoweb.
- Support for `mip` HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from picoweb.