Tag Archives: python

mjml + jinja2 = awesome

If you have ever tried programmatically sending HTML emails, you probably know how giant of a pain in the rear it is. It’s all because while HTML has progressed in the world of Internet, the Email HTML is still way way behind. There are no set standards and most of the times, you end up doing too much manual HTML coding. In today’s world, surely there must be something you can do about it, right? Turns out, yes. MJML tries to standardize and speed up HTML email layout creation. Think of it as Bootstrap for Email. MJML comes with it’s tagging format. The resulting layouts are very cross-platform and responsive, just like today’s modern websites are. Granted, things can never be 100% perfect in the Email world, but MJML gets you to at least 90% there.

Then, if you are using Python you can make Emails dynamic using Jinja library. There is some work involved in tying the two together, i.e. making MJML and Jinja work seamlessly, but once set up, they work very very nicely.

To be continued…

PyCharm on Windows and Docker

I have recently started using Python and Docker. As a novice, I wanted to configure PyCharm on Windows to use my Docker Python image. Here are couple of things I had to do to successfully accomplish this:

  • Your Project folder must be inside your Windows home folder, i.e. c:\users\user. If not, PyCharm will not be able to run your Python file

2016-06-16 21_02_45-Location

  • Next, ensure that Docker is selected as a “Remote Python Interpreter”. Obviously as a prerequisite you must install Docker and pull Python image. If done correctly, PyCharm should recognize the interpreter

2016-06-16 21_01_10-ConfigureDocker

With this you should be able to run your Python code directly against Docker image. Here’s a Hello World example I ran.

2016-06-16 21_04_28-HelloWorld - [C__Users_vikc0_Python_HelloWorld] - ..._hello_world.py - PyCharm 5

Playing with wit.ai

wit.ai is a NLP (Natural Language Processing) api. Think of it as something that can be used to create next Cortana or Siri. Basically, the api allows you to send a text statement (or voice), and convert into meaningful form that your program can understand and respond to.

I have started playing with it. First thing I am doing is install on Jarvis to be used with Python3.

Installing is easy. First ensure following packages are installed: python3-dev, libcurl3-dev, libsox-dev and libssl-dev.

Then run pip

..to be continued

Trick 2: Bulk download 4K wallpapers from alphacoders.com

A year ago I had posted about how you can bulk download 1080p wallpapers from Microsoft’s website. You can read the post here. That tricked worked very well and allowed me to download a total of 543 1080p wallpapers in one go. Now, I am using the same concept to download wallpapers from alphacoders.com albeit this time it’s 4K, baby! (3840×2160)

Since the level of our pixel gorgeousness has increased we will step up our game a bit by using a Python script to automate the download. Actually, that’s not the main reason… While using lynx, grep and wget worked previously, it may be a bit untidy here. We are dealing with 117 pages full of 4K wallpapers. I estimate around 3000+ wallpapers here. That’s >6 times as previously.

Using Python requests, BeautifulSoup and shutil, it’s very easy. Here’s how I did it.

DISCLAIMER: The code here is only for educational purposes. Use it at your own risk. I am thankful to the original posters of these 4K wallpapers. They deserve due credit.
1. Construct the page URL that shows thumbnails for 4K wallpapers
The first page is this: http://wall.alphacoders.com/by_resolution.php?w=3840&h=2160
Page 2 is this: http://wall.alphacoders.com/by_resolution.php?w=3840&h=2160&page=2
…you get the pattern, right?

2.  On the page, look for image tags with ‘alt’ attribute starting with ‘HD Wallpaper’

3. Loop through all the thumbnails, extract their ‘src’ values and strip ‘thumb-350-‘ string from the ‘src’ values to get the actual 4K wallpaper URL
Input = http://images.alphacoders.com/488/thumb-350-488146.jpg
Output = http://images.alphacoders.com/488/488146.jpg

4. Download the image and store in a file

5. Loop through all the pages

Complete snippet:

That’s it!


Here are stats for my download process. Obviously speed stinks but I was able to 99.9% of 4K Wallpapers.

Downloaded: 3501 Skipped: 5
Total size: 8.66gb Speed: 370.86kb/s Elapsed time:  6h 48m 18.7s