Archive

Archive for October, 2016

pytumblr with Python 3 support

October 22, 2016 Leave a comment

Problem
I wanted to use the pytumblr library from Python 3 but this library is Python 2 only :( And the repo is quite abandoned. Someone asked Python 3 support more than a year ago and nothing happened.

Solution
As I had a large Python 3 project and I wanted to integrate Tumblr support in it, I decided to modify the library to support Python 3. The result is here: https://github.com/jabbalaci/pytumblr . I only needed to upload photos, so I worked on that part only. But with this version you can upload photos to Tumblr under Python 3.

Categories: python Tags: , , ,

[matplotlib] create figures on a remote server

October 16, 2016 Leave a comment

Problem
On a remote server of mine I wanted to create some nice figures with matplotlib. It worked well on localhost but it failed on the server. First, tkinter was missing. Second, there was a problem with $DISPLAY.

Solution
To install tkinter, check out this earlier post.

The second problem is caused by a missing X server. On a remote server usually there is no graphical interface. To solve it, just add these two lines to the top of your program:

import matplotlib as mpl
mpl.use('Agg')

Example
Before:

#!/usr/bin/env python3
# coding: utf-8

import pylab
import numpy as np

def main():
    x = np.arange(-3.14, 3.14, 0.01)
    y = np.sin(x)
    pylab.plot(x, y, "b")
    pylab.savefig("out.png")

##############################################################################

if __name__ == "__main__":
    main()

After:

#!/usr/bin/env python3
# coding: utf-8

import matplotlib as mpl
mpl.use('Agg')

import pylab
import numpy as np

def main():
    x = np.arange(-3.14, 3.14, 0.01)
    y = np.sin(x)
    pylab.plot(x, y, "b")
    pylab.savefig("out.png")

##############################################################################

if __name__ == "__main__":
    main()

Output:
sine

Categories: matplotlib, python Tags: , , ,