Covid 19 Updates from Vim, with Python!

pcvonz

Paul

Posted on March 13, 2020

Covid 19 Updates from Vim, with Python!

Alt Text
This was just a fun (and morbid) way to experiment with hacking on vim with Python.

If you have Vim compiled with Python and the Airline plugin installed, you should be able to just plop this into your vimrc:

💡 There is an updated version that works asynchronously below.

function! CovidUpdate()
python3 << EOF
from urllib import request
import json
import vim
def getCases():
  country = "US"
  res = request.urlopen("https://covid2019-api.herokuapp.com/country/%s" % country)
  string = res.read().decode()
  info = json.loads(string)[country]
  vim.vars["response"] = "Country: %s | Confimred:  %i | Deaths: %i | Recovered: %i " %  (country, info["confirmed"],  info["deaths"], info["recovered"])
getCases()
EOF

endfunction

call CovidUpdate()


" call airline#parts#define_function('foo', "CovidUpdate")
let g:airline_section_y = airline#section#create_right(['ffenc', response])

Enter fullscreen mode Exit fullscreen mode

Thanks to this project for providing the API.

🚨 UPDATE 🚨

Threading a bit of python via the vim module is actually really easy!

Updated version:

function! CovidUpdate()
python3 << EOF
from urllib import request
import threading
import json
import vim
def getCases():
  country = "US"
  res = request.urlopen("https://covid2019-api.herokuapp.com/country/%s" % country)
  string = res.read().decode()
  info = json.loads(string)[country]
  vim.vars["airline_section_y"] = "Country: %s | Confimred:  %i | Deaths: %i | Recovered: %i " %  (country, info["confirmed"],  info["deaths"], info["recovered"])
vim.async_call(getCases)
EOF

endfunction

call CovidUpdate()
Enter fullscreen mode Exit fullscreen mode

Note, the last line in the previous section:
let g:airline_section_y = airline#section#create_right(['ffenc', response])
is no longer necessary since that variable is set in the Python code. 🧠

💖 💪 🙅 🚩
pcvonz
Paul

Posted on March 13, 2020

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related