├── requirements.txt ├── LICENSE ├── cli.py └── README.md /requirements.txt: -------------------------------------------------------------------------------- 1 | requests==2.18.4 2 | click==6.0 3 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 Sebastian Vetter 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /cli.py: -------------------------------------------------------------------------------- 1 | import click 2 | import requests 3 | 4 | SAMPLE_API_KEY = 'b1b15e88fa797225412429c1c50c122a1' 5 | 6 | 7 | def current_weather(location, api_key=SAMPLE_API_KEY): 8 | url = 'https://api.openweathermap.org/data/2.5/weather' 9 | 10 | query_params = { 11 | 'q': location, 12 | 'appid': api_key, 13 | } 14 | 15 | response = requests.get(url, params=query_params) 16 | 17 | return response.json()['weather'][0]['description'] 18 | 19 | 20 | @click.command() 21 | @click.argument('location') 22 | @click.option( 23 | '--api-key', '-a', 24 | help='your API key for the OpenWeatherMap API', 25 | ) 26 | def main(location, api_key): 27 | """ 28 | A little weather tool that shows you the current weather in a LOCATION of 29 | your choice. Provide the city name and optionally a two-digit country code. 30 | Here are two examples: 31 | 1. London,UK 32 | 2. Canmore 33 | You need a valid API key from OpenWeatherMap for the tool to work. You can 34 | sign up for a free account at https://openweathermap.org/appid. 35 | """ 36 | weather = current_weather(location, api_key) 37 | print(f"The weather in {location} right now: {weather}.") 38 | 39 | 40 | if __name__ == "__main__": 41 | main() 42 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Click Tutorial Example 2 | 3 | This repo contains the Click example that I'm using throughout my written tutorials. You can check out the different stages of the tutorial by checking out the corresponding branches. `master` will always contain the latest, most complete version of the example. 4 | 5 | 6 | ## Getting Started 7 | 8 | You can easily get started with this example by cloning this repository and installing the dependencies. My recommendation would be to do that in a virtual environment. You can find out more [on virtual environment in the official documentation for Python](https://docs.python.org/3/tutorial/venv.html). 9 | 10 | This example **requires Python 3.6**. That means you can use the built-in virtual environment module `venv` that comes with Python 3.6. 11 | 12 | Here's what the setup of this project looks like starting from scratch: 13 | 14 | ```sh 15 | # Clone the Github repo 16 | $ git clone git@github.com:elbaschid/click-example.git 17 | # Change into the project directory 18 | $ cd click-example 19 | # Create a virtual environment with Python 3 20 | $ python3 -m venv ./venv 21 | # Activate the virtual environment 22 | $ source ./venv/ 23 | # Install the required packages 24 | (venv) $ pip install -r requirements.txt 25 | ``` 26 | 27 | ## Contributions 28 | 29 | I'm only human, which means I make mistakes. If you find anything that doesn't work or think you can phrase things clearer, feel free to **open an issue**. Or if you have the skills to fix it, **fork the repo** and **open a pull request** with your suggested changes. 30 | 31 | I appreciate any constructive input 😍. 32 | 33 | # License 34 | 35 | This project is licensed under the [MIT license](./LICENSE). Basically, you can do whatever you want with it 💖. 36 | --------------------------------------------------------------------------------