This post walks through writing an interactive command-line app in Python. I’m a big fan of command-line apps: they keep me productive, and navigating them is a lot easier than clicking through a GUI.
We’ll build a simple command-line app that uses the Yelp API to find a restaurant in your city.
Let’s get started
- Make sure you have
python3installed. - Create a virtual environment with
pipenvto handle the Python dependencies.
pipenv shell If you don’t have pipenv installed, follow the pipenv installation guide.
- After activating the shell, install the packages. For this app I’m using PyInquirer, a package that helps you build interactive command-line apps.
pipenv install PyInquirer pipenv install prompt_toolkit==1.0.14 Once
PyInquireris installed, install the Python implementation of the Yelp Fusion API:pipenv install yelpapi. YelpApi (Python implementation)Everything is ready.
Writing the program
from __future__ import print_function, unicode_literals
import os
import emoji
from PyInquirer import style_from_dict, Token, prompt, Separator
from examples import custom_style_2
import json
from termcolor import colored
from yelpapi import YelpAPI
# Api key from Yelp Developers
api_key = os.environ['API_KEY']
## This allows you create prompts for the user.
questions = [
{
'type': 'input',
'name': 'food',
'message': emoji.emojize('What are you looking to eat ? :sushi:,:pizza:,:hamburger:'),
},
## Allows you have a list of choices
## Furthermore you can refer to the documentation.
{
'type': 'list',
'name': 'city',
'message': 'Choose your City',
'choices': [
"New York City",
"Los Angeles",
"Chicago",
"Houston",
"Philadelphia",
"Phoenix",
"San Antonio",
"San Diego",
"Dallas",
"San Jose",
"Austin",
"Jacksonville",
"San Francisco",
"Indianapolis",
"Columbus",
"Fort Worth",
"Charlotte",
"Detroit",
"El Paso",
"Seattle ",
"Denver ",
"Washington",
"Memphis",
"Boston ",
"Nashville-Davidson",
"Baltimore",
"Not Listed"
]
},
]
## This will allow you get the results from Yelp Api based on user's input.
answers = prompt(questions, style=custom_style_2)
if answers['city'] == 'Not Listed':
x = input("What\'s your city: ")
search_results = yelp_api.search_query(
term=answers['food'],
location=answers['city'],
sort_by='rating',
limit=5)
print('Here are few restaurants I found ')
for nums in search_results['businesses']:
print('Name : ' +
colored(nums['name'], 'white', attrs=['bold']) +
'\n' +
'Phone: ' +
colored(nums['phone'], 'green', attrs=['bold']) +
'\n' +
'Address: ' +
colored(nums['location']['address1'],'cyan',attrs=['bold']) +
'\n' +
colored('Yelp url: ','red',attrs=['bold']) +
nums['url']
)
else:
search_results = yelp_api.search_query(
term=answers['food'],
location=answers['city'],
sort_by='rating',
limit=5)
print('Here are few restaurants I found ')
for nums in search_results['businesses']:
print('Name : ' +
colored(nums['name'], 'white', attrs=['bold']) +
'\n' +
'Phone: ' +
colored(nums['phone'], 'green', attrs=['bold']) +
'\n' +
'Address: ' +
colored(nums['location']['address1'],'cyan',attrs=['bold']) +
'\n' +
colored('Yelp url: ','red',attrs=['bold']) +
nums['url']
)- To run the program, get API keys from Yelp Developers.
- Once you have the keys, export them:
export API_KEY=XXXXXXX - That’s it: you’ve written your first interactive CLI app.
This was my first blog post after a long break; I’d been busy with school.
If I missed anything, feel free to DM me on Twitter. Feel free to share this with friends and colleagues.