Is it possible to utilize a CLI (module/app/library) for use in my own Python script?

Question:

I am fairly new to coding but love Python and am trying to understand something as I’ve run into a specific issue I need solved!

To give an example for context –

I am currently trying to stylize a pyfiglet title (centered) as a colored gradient. There seems to be no way to do this ASIDE FROM:

https://github.com/wasi-master/gradient_figlet

However, this is a CLI module/tool and I have no idea how to implement it into my script. There is no documentation and I am used to being provided with examples such as:

import gradient-figlet

gradient_figlet("TEST HELLO")

To give a printed gradient text (figlet) result as gradient-figlet accomplishes when used in command line using:

python -m gradient_figlet YOUR_TEXT

Is this possible in general?

Any help would be appreciated.

On a side note – I’d really like to be able to center things in console if anyone has any tips 🙂

Asked By: KEEP BUSY

||

Answers:

It doesn’t look like gradient_figlet has a non CLI implementation. However you could use subprocess to run the CLI and capture the output and use it.

import subprocess

the_result = subprocess.run([‘python’, ‘-m’, ‘gradient_figlet’, ‘YOUR_TEXT’], stdout=subprocess.PIPE)

Let me know if this isn’t what you are looking for!

Answered By: Travis Simmons

I spoke with the creator of the repo and he kindly updated the repo to support usage within a script!

https://github.com/wasi-master/gradient_figlet

Hope this helps someone!

Answered By: KEEP BUSY

I’m the author of the library and I’ve since added a way to print gradient figlets programmatically. I initially wanted to add this in the initial release but it got delayed due to reasons.

You can use the print_with_gradient_figlet function to achieve this.

import gradient_figlet

gradient_figlet.print_with_gradient_figlet("TEST HELLO", "#00ff00", "#ff0000")

The first parameter is the text and the next 2 ones are from_color and to_color There are a few optional parameters that you can check in the source.

Answered By: Wasi Master