Building interactive command-line interfaces is a powerful way to engage with users directly from the terminal. I’ve come across two great libraries for this purpose: Questionary (for Python) and Clack (for JavaScript). Let’s explore these tools and their use cases, especially how they can be combined with my projects for reading PDFs, fetching YouTube transcripts, and summarizing content.


Questionary (Python)

Questionary is a Python library that allows you to create interactive command-line prompts with minimal effort. It supports a wide range of input types like text, password, select, multi-select, confirm, and more. You can even execute commands or processes between prompts.

Example 1: Basic Usage

Here’s a simple example that demonstrates asking the user for input and then performing some action between prompts.

import questionary
import time
 
# First prompt
name = questionary.text("What's your name?").ask()
 
# Simulating a task
print(f"Hello, {name}! Let me fetch some data...")
time.sleep(2)
 
# Second prompt after fetching data
age = questionary.text("How old are you?").ask()
 
print(f"Got it! You're {age} years old.")

This flexibility makes Questionary perfect for tasks where you need user interaction and also have background processing between prompts.


Example 2: Fetching YouTube Transcript

One of my projects fetches YouTube transcripts, and we can easily integrate Questionary to interact with the user in the terminal, allowing them to decide whether to fetch the transcript or perform another action.

import questionary
from my_youtube_transcript_fetcher import fetch_transcript  # Hypothetical module
 
# Ask for YouTube video ID
video_id = questionary.text("Enter the YouTube video ID:").ask()
 
# Fetch transcript
transcript = fetch_transcript(video_id)
print("Transcript fetched successfully!")
 
# Ask if the user wants to save it to clipboard
save_to_clipboard = questionary.confirm("Do you want to save the transcript to the clipboard?").ask()
 
if save_to_clipboard:
    # Assume we have a function to copy to clipboard
    copy_to_clipboard(transcript)
    print("Transcript copied to clipboard!")

Example 3: PDF Processing and Summarization

In another project, I extract text from PDFs and generate summaries. Let’s imagine a scenario where we ask the user if they want to save the summary or output it to the console.

import questionary
from my_pdf_summary_project import summarize_pdf  # Hypothetical module
 
# Ask for PDF file path
pdf_path = questionary.text("Enter the path to the PDF file:").ask()
 
# Generate the summary
summary = summarize_pdf(pdf_path)
print("Summary generated successfully!")
 
# Ask how to proceed with the summary
action = questionary.select("What would you like to do with the summary?", choices=["Print to console", "Save to file", "Copy to clipboard"]).ask()
 
if action == "Print to console":
    print(summary)
elif action == "Save to file":
    with open("summary.txt", "w") as f:
        f.write(summary)
    print("Summary saved to summary.txt!")
elif action == "Copy to clipboard":
    copy_to_clipboard(summary)
    print("Summary copied to clipboard!")

Clack (JavaScript)

I first encountered command-line interactions through Clack in the JavaScript ecosystem. Although we’re focusing on Python here, it’s worth noting that Clack offers similar functionality for creating rich prompts and interactions in JavaScript applications. Quartz (a project worth checking out) uses it to enhance the interactive experience in their toolset.


RasaHQ: Inspiration for Using Prompts

While Questionary and Clack are great for creating interactive command-line interfaces, it’s worth mentioning that complex projects like RasaHQ also heavily rely on prompt-based interactions. Rasa builds conversational AI applications, and the command-line experience for developers is a key part of its usability.


Conclusion

Both Questionary and Clack provide excellent options for building dynamic, interactive command-line tools. Whether it’s fetching YouTube transcripts, reading and summarizing PDFs, or interacting with OpenAPI from the terminal, these tools offer flexibility and ease of use. For those working in JavaScript, Clack and Quartz showcase how rich terminal interactions can elevate your project. For Python developers, Questionary can be a powerful companion in building engaging and functional CLI tools.

Remember to check out my other projects that integrate with Questionary:

  • PDF summarization
  • YouTube transcript fetching
  • Clipboard management for command-line outputs

Happy coding!


References: