tornado
How to redirect on another page and pass parameter in url from table?
Set the user name as data-username attribute to the button and also a class: HTML <input type=”button” name=”theButton” value=”Detail” class=”btn” data-username=”{{result[‘username’]}}” /> JS $(document).on(‘click’, ‘.btn’, function() { var name = $(this).data(‘username’); if (name != undefined && name != null) { window.location = ‘/player_detail?username=” + name; } }); EDIT: Also, you can simply check for undefined … Read more
How to get image size (bytes) using PIL
Try: import os print os.stat(‘somefile.ext’).st_size
Python JSON encoder to support datetime?
json.dumps(thing, default=str)
How to run Flask app with Tornado
The Flask docs used to describe how to do this, but it has been removed due to the performance notes below. You don’t need Tornado to serve your Flask app, unless all your async code is already written in Tornado. The Tornado docs about WSGI describe this as well. They also include a big warning … Read more
saving picture to mongodb
You don’t necessarily need GridFS for storing files in MongoDB, but it surely makes it a nicer experience, because it handles the splitting and saving of the binary data, while making the metadata also available. You can then store an ID in your User document to the avatar picture. That aside, you could also store … Read more
Is JSON Hijacking still an issue in modern browsers?
No, it is no longer possible to capture values passed to the [] or {} constructors in Firefox 21, Chrome 27, or IE 10. Here’s a little test page, based on the main attacks described in http://www.thespanner.co.uk/2011/05/30/json-hijacking/: (http://jsfiddle.net/ph3Uv/2/) var capture = function() { var ta = document.querySelector(‘textarea’) ta.innerHTML = ”; ta.appendChild(document.createTextNode(“Captured: “+JSON.stringify(arguments))); return arguments; } … Read more
Jupyter Notebook with Python 3.8 – NotImplementedError
EDIT This issue exists in older versions of Jupyter Notebook and was fixed in version 6.0.3 (released 2020-01-21). To upgrade to the latest version run: pip install notebook –upgrade Following on this issue through GitHub, it seems the problem is related to the tornado server that jupyter uses. For those that can’t wait for an … Read more
How can I periodically execute a function with asyncio?
For Python versions below 3.5: import asyncio @asyncio.coroutine def periodic(): while True: print(‘periodic’) yield from asyncio.sleep(1) def stop(): task.cancel() loop = asyncio.get_event_loop() loop.call_later(5, stop) task = loop.create_task(periodic()) try: loop.run_until_complete(task) except asyncio.CancelledError: pass For Python 3.5 and above: import asyncio async def periodic(): while True: print(‘periodic’) await asyncio.sleep(1) def stop(): task.cancel() loop = asyncio.get_event_loop() loop.call_later(5, stop) … Read more
how to show non ASCII character using python [closed]
gs.translate(‘this is a pen’,’bn’) produces a Unicode string. If you just type gs.translate(‘this is a pen’,’bn’) into the interactive interpreter it prints the representation of that string which is u’\u098f\u0987 \u098f\u0995\u099f\u09bf \u0995\u09b2\u09ae’. But when you type print(gs.translate(‘this is a pen’,’bn’)) the Unicode data is encoded into a stream of bytes using the default encoding (which … Read more