The i3 FAQ has migrated to https://github.com/i3/i3/discussions. All content here is read-only.
Ask Your Question
1

Why can I send input to i3bar with bash script, but not with python?

asked 2015-07-13 13:28:38 +0000

this post is marked as community wiki

This post is a wiki. Anyone with karma >100 is welcome to improve it.

As a test, I added this to my .i3/config:

bar {
    status_command i3status | ~/.i3/status.sh
}

with the script:

#!/bin/bash

echo '{ "version": 1 }'
echo '['
echo '[]'
while :;
do
    echo ",[{\"name\":\"time\",\"full_text\":\"test\"}]"
    sleep 1
done

I see "test" on the i3bar. Nice. However, I want to use python for the real script, so I tried:

bar {
    status_command i3status | ~/.i3/status.py
}

with:

#!/usr/bin/env python
import time

print '{ "version": 1 }'
print '['
print '[]'
while True:
    print ',[{"name":"time","full_text":"test"}]'
    time.sleep(1)

but... no joy. Nothing on the i3bar. Running the scripts in terminal, the output looks the same. Even output to file and examined in hex, and it was the same. Is it some sort of problem with buffering? Even if I wait, nothing shows up on the bar. Tried running with "python ~/.i3/status.py", still nothing. Am I missing something obvious? (That does happen to me a lot.) Thanks in advance!

edit retag flag offensive close merge delete

Comments

Yes, it's buffering. Try using "stdbuf -o 0 -e 0 python2.7 ~/.i3/status.py" and you can see it works.

Airblader gravatar imageAirblader ( 2015-07-13 13:53:21 +0000 )edit

Thanks! Looks like you just beat me to it. Can't upvote, but props for the quick reply. There's a good discussion on the issue here (explains why it all looked fine in terminal): http://stackoverflow.com/questions/15731231/python-script-prints-output-of-os-system-before-print

will_82 gravatar imagewill_82 ( 2015-07-13 13:59:09 +0000 )edit

To be fair, stdbuf only meant to prove the theory, not to be a solution, so it wasn't an answer. Using "-u" is the right solution :)

Airblader gravatar imageAirblader ( 2015-07-13 16:13:37 +0000 )edit

1 answer

Sort by ยป oldest newest most voted
1

answered 2015-07-13 13:53:21 +0000

this post is marked as community wiki

This post is a wiki. Anyone with karma >100 is welcome to improve it.

Aghhh... just figured it out... it was a buffering problem. Needed to launch with

bar {
    status_command i3status | python -u ~/.i3/status.py
}

This makes python send unbuffered output. Hope this helps someone else who's playing around with the i3bar and python. I've been pulling out my hair all day!

edit flag offensive delete link more

Question Tools

Stats

Asked: 2015-07-13 13:28:38 +0000

Seen: 212 times

Last updated: Jul 13