Repository: ManimCommunity/manim

Issue:
Feature request: optionally suppress "Manim Comunity v{version}"
## Description of proposed feature

Hello! As of recent versions of Manim, it is not possible to suppress the `Manim Community v{version}` message that is printed out when importing `manim`.

Actually, this is because importing `manim` imports `manim.__init__`, which in turn imports `manim.__main__` with this: https://github.com/ManimCommunity/manim/blob/88836df8ab1ea153ed57848a74a694c517962163/manim/__init__.py#L104-L113

Before, I could suppress the message using some redirect, but now if fails

```python
>>> import os
>>> from contextlib import redirect_stdout
>>> 
>>> with redirect_stdout(open(os.devnull, "w")):
>>>     print("HELLO")
>>>     import manim
Manim Community v0.17.2
```
because Rich will still print to `stdout` (since the console was created before `stdout` was redirected). 

Rich's console has an optional `file` argument, but there currently seem to be a bug with it, reported in https://github.com/Textualize/rich/issues/3083, that makes Rich's consoles only print to stdout.

A **nice feature** would be to make the behavior optional.

## How can the new feature be used?

There are a few possible solutions:

- Not printing the version at all;
- Moving the print message inside the `__main__`:
https://github.com/ManimCommunity/manim/blob/88836df8ab1ea153ed57848a74a694c517962163/manim/__main__.py#L59-L60
- Optionally print (or disable print) with some environ variable
- ...

## Additional comments

I develop Manim Slides, a Manim plugin, and I'd like to avoid printing the Manim Community version every time I import this package, because the user does not really need to know that.



Discussion:
You can just make a PR for that if you want, i would still say it is useful for your users to know the version, because as soon as they have problems with the Plugin you will want to know the version number.
With the current version 0.17.3 I don't observe the problem you describe here - the redirection suppresses the message. Was this perhaps a flaw in 0.17.2?

![bild](https://github.com/ManimCommunity/manim/assets/8582807/7731a01e-57bf-45a7-a318-89fe4ff68d66)

Thank you for the suggestion! The version banner is (unfortunately) rather helpful for the helpers in our forum because it is a quick way to tell whether people are invoking manim vs. manimlib, so I think we'll want to preserve the current default behavior -- and as a consequence of this, the priority for working on this is rather low. Contributions are welcome. :-)

Regarding your suggestions:

- Not printing the version is not ideal (as others have already mentioned too),
- I am not sure whether the `__main__` logic in that file really does anything: the `main` function defined above doesn't do anything, and the `__name__` seems to be set to `manim.__main__` whenever manim is not invoked via the module directly (i.e., via `python -m manim`).
- It's certainly possible to introduce a `--hide-splash` or something along these lines to suppress the version.

Thanks for your work on Manim slides!
@behackl and @MrDiver I totally agree with that printing the version has nice pros.

But imo the version printing could be limited to the CLI command (and not the module itself).

I will do a PR when I have some time :)
@uwezi i dis not check when the console printing was changed, but the version is only printed if you have IPython installed if I understand correctly.
when I remove the redirection from your example and run the script the version is printed to the console even without using IPython - so for me your hack works...

Resolving patch:
diff --git a/manim/__main__.py b/manim/__main__.py
--- a/manim/__main__.py
+++ b/manim/__main__.py
@@ -2,6 +2,7 @@
 
 import sys
 
+import click
 import cloup
 
 from . import __version__, cli_ctx_settings, console
@@ -14,12 +15,15 @@
 from .constants import EPILOG
 
 
-def exit_early(ctx, param, value):
+def show_splash(ctx, param, value):
     if value:
-        sys.exit()
+        console.print(f"Manim Community [green]v{__version__}[/green]\n")
 
 
-console.print(f"Manim Community [green]v{__version__}[/green]\n")
+def print_version_and_exit(ctx, param, value):
+    show_splash(ctx, param, value)
+    if value:
+        ctx.exit()
 
 
 @cloup.group(
@@ -37,7 +41,16 @@ def exit_early(ctx, param, value):
     "--version",
     is_flag=True,
     help="Show version and exit.",
-    callback=exit_early,
+    callback=print_version_and_exit,
+    is_eager=True,
+    expose_value=False,
+)
+@click.option(
+    "--show-splash/--hide-splash",
+    is_flag=True,
+    default=True,
+    help="Print splash message with version information.",
+    callback=show_splash,
     is_eager=True,
     expose_value=False,
 )


Test patch:
diff --git a/tests/interface/test_commands.py b/tests/interface/test_commands.py
--- a/tests/interface/test_commands.py
+++ b/tests/interface/test_commands.py
@@ -28,7 +28,9 @@ def test_manim_cfg_subcommand():
     command = ["cfg"]
     runner = CliRunner()
     result = runner.invoke(main, command, prog_name="manim")
-    expected_output = """\
+    expected_output = f"""\
+Manim Community v{__version__}
+
 Usage: manim cfg [OPTIONS] COMMAND [ARGS]...
 
   Manages Manim configuration files.
@@ -50,7 +52,9 @@ def test_manim_plugins_subcommand():
     command = ["plugins"]
     runner = CliRunner()
     result = runner.invoke(main, command, prog_name="manim")
-    expected_output = """\
+    expected_output = f"""\
+Manim Community v{__version__}
+
 Usage: manim plugins [OPTIONS]
 
   Manages Manim plugins.
@@ -90,7 +94,9 @@ def test_manim_init_subcommand():
     command = ["init"]
     runner = CliRunner()
     result = runner.invoke(main, command, prog_name="manim")
-    expected_output = """\
+    expected_output = f"""\
+Manim Community v{__version__}
+
 Usage: manim init [OPTIONS] COMMAND [ARGS]...
 
   Create a new project or insert a new scene.