DEV Community

Cover image for Covid 19 Updates from Vim, with Python!
Paul
Paul

Posted on

3 2

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. 🧠

Image of Datadog

Create and maintain end-to-end frontend tests

Learn best practices on creating frontend tests, testing on-premise apps, integrating tests into your CI/CD pipeline, and using Datadog’s testing tunnel.

Download The Guide

Top comments (1)

Collapse
 
pcvonz profile image
Paul

The API seems to be a little buggy. It's returning zero cases in the US. Kinda nice :)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay