imgix / imgix-python Goto Github PK
View Code? Open in Web Editor NEWA Python client library for generating URLs with imgix
Home Page: https://www.imgix.com
License: BSD 2-Clause "Simplified" License
A Python client library for generating URLs with imgix
Home Page: https://www.imgix.com
License: BSD 2-Clause "Simplified" License
At Rover we are upgrading our codebase to python3.6 while maintaining compatibility with python2.7.
We are running into a problem where imgix isn't compatible with both python 2.7 and 3.6 at the same time.
The reason is that the behavior of string literals in the imgix-python library is not consistent between python versions.
For example, take URLBuilder.create_url(path, opts)
. If path
is explicitly unicode in python 2, this will result in the following error:
File "/usr/local/lib/python2.7/dist-packages/imgix/urlhelper.py", line 188, in __str__
"", ])
File "/usr/local/lib/python2.7/dist-packages/future/backports/urllib/parse.py", line 387, in urlunparse
_coerce_args(*components))
File "/usr/local/lib/python2.7/dist-packages/future/backports/urllib/parse.py", line 115, in _coerce_args
raise TypeError("Cannot mix str and non-str arguments")
TypeError: Cannot mix str and non-str arguments
This will fail in the same way if path
is explicitly a byte string in python 3. The only way that it can be expected to work at all between the two versions is if the path
variable is a string literal with no explicit type and from __future__ import unicode_literals
is not present.
In order to ease our transition of hundreds of thousands of lines of code to python3 we need to include all future imports in all our files which means that path
will be explicitly unicode in both python2 and python3.
The issue can be fixed by making string literals be either explicitly unicode or bytestring regardless of what version of python was being run. This can be done by adding from __future__ import unicode_literals
to all the files in this library.
One way to automatically do this would be to use futurize
When signing a url with a +
encoded as %20
for a web proxy secure source, the signing fails.
Try encoding:
http://www.brookings.edu/~/media/research/images/l/lk%20lo/london_bigben_flags001/london_bigben_flags001_16x9.jpg
>>> import imgix
>>> image_url_builder = imgix.UrlBuilder(IMGIX_URL, sign_key=IMGIX_SIGN_KEY, use_https=True)
>>> image_url = 'http://www.brookings.edu/~/media/research/images/l/lk%20lo/london_bigben_flags001/london_bigben_flags001_16x9.jpg'
>>> options = {'w': 260, 'fit': 'crop', 'crop': 'entropy'}
>>> image_url_builder.create_url(image_url, options)
'https://factr-url.imgix.net/http%3A%2F%2Fwww.brookings.edu%2F%7E%2Fmedia%2Fresearch%2Fimages%2Fl%2Flk%2520lo%2Flondon_bigben_flags001%2Flondon_bigben_flags001_16x9.jpg?crop=entropy&fit=crop&ixlib=python-1.1.0&w=260&s=473639e97d47f1b991e46ba670042358'
The signed url throws a 401.
Related to https://github.com/imgix/js-core#fixed-image-rendering
Add a device_pixel_ratios
option to the https://github.com/imgix/imgix-python#fixed-width-images function to allow users to customize the target device pixel ratio(s) (dpr
) when generating a fixed image. This will allow users to have more granular control over the fixed-size srcset values generated.
The value is named SRCSET_INCREMENT_PERCENTAGE
, consider making it one.
imgix-python/imgix/constants.py
Line 8 in 6f92af1
It's only other use is 13 lines down here,
imgix-python/imgix/constants.py
Line 21 in 6af3a08
where the conversion to a percentage actually occurs.
Reasons to Consider:
Operative line to update: https://github.com/imgix/imgix-python/blob/master/imgix/__init__.py#L79
See also the case in the blueprint: https://github.com/imgix/imgix-blueprint#fully-qualified-urls-with-imgix-url-api-parameters
import imgix
b = imgix.url.Builder("demos.imgix.net")
b.create("/bridge.png", w=100, h=100)
Is Helper needed? It's not currently exposed in the docs.
The library is encoding the space in the dl as %20
, but if you put a signed url in an anchor <a href="url">
it gets changed by the browser to a +
, resulting in the error message "URI signature match failed".
There are many settings that can be used with Imgix. These have a range of types, but most are integers, floats, or strings that define an enumeration.
It would be great to get a more type-safe API using Python enums and/or mypy types. This would also give some basis to further safety in APIs provided using this library (such as a GraphQL API backed by this).
Related PR: imgix/js-core#314
Add disablePathEncoding
functionality to the URL building logic in create_url
and create_srcset
. This way, users can decide to opt-out of path encoding.
Probably a good way of handling this would be to add a new constructor option, disablePathEncoding
, that can be referenced in any of the instance methods.
Related to https://github.com/imgix/js-core#variable-qualities
Add a variable_qualities
option to the https://github.com/imgix/imgix-python#fixed-width-images function to allow users to override the mapping of the quality parameter (q
) to each device pixel ratio (dpr
). This will allow users to have more granular control over the fixed-size srcset values generated.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.