
    lj!                       d Z ddlmZ ddlZddlZddlmZ ddlmZ ddlm	Z	 ddl
mZmZmZ ddlmZ dd	lmZ d
dlmZ dgZ	 ddlmZ ddlmZ ddlmZmZmZmZ ddlmZm Z m!Z! e G d de                      Z"n# e#$ r Y nw xY wddZ$dS )zIUtilities for using Manim with IPython (in particular: Jupyter notebooks)    )annotationsN)datetime)Path)Any)configlogger
tempconfig)main)shader_program_cache   )RendererType
ManimMagic)get_ipython)InteractiveShell)Magicsline_cell_magicmagics_classneeds_local_scope)ImageVideodisplayc                  V     e Zd Zd fdZee	 	 ddd                        ZddZ xZS )r   shellr   returnNonec                X    t                                          |           i | _        d S )N)super__init__rendered_files)selfr   	__class__s     T/home/agentuser/manim-venv/lib/python3.11/site-packages/manim/utils/ipython_magic.pyr   zManimMagic.__init__#   s)    GGU###46D    Nlinestrcell
str | Nonelocal_nsdict[str, Any] | Nonec                   |rt          ||           |                                }t          |          rd|v sd|v sd|v rt          |dd           dS |                     |          }t          |dd          }t          |t                    sJ t          |                    di                     5  t          j
        |           d}t          j        t          j        k    rd	d
lm}  |            }	 |t          d         d	                  } ||          }	|	                                 t#          j                     | |j        |j                                         n<# t#          j                     |!|j        |j                                         w w w xY wt          d         "t+          j        d           	 ddd           dS t/          t          d                                       t/          j                              }
t/          t          d                   dz  t5                       |
j         z  }|
| j        v r| j        |
                                          || j        |
<   |j                            dd           tA          j!        |
|           tE          j#        t          d                   d	         }t          |tH                    sJ t          d         }|sdtI          tK                                v }|&                    d          rtO          t          d                   }n!tQ          |dt          d          d|          }tS          |           ddd           dS # 1 swxY w Y   dS )a  Render Manim scenes contained in IPython cells.
            Works as a line or cell magic.

            .. hint::

                This line and cell magic works best when used in a JupyterLab
                environment: while all of the functionality is available for
                classic Jupyter notebooks as well, it is possible that videos
                sometimes don't update on repeated execution of the same cell
                if the scene name stays the same.

                This problem does not occur when using JupyterLab.

            Please refer to `<https://jupyter.org/>`_ for more information about JupyterLab
            and Jupyter notebooks.

            Usage in line mode::

                %manim [CLI options] MyAwesomeScene

            Usage in cell mode::

                %%manim [CLI options] MyAwesomeScene

                class MyAweseomeScene(Scene):
                    def construct(self):
                        ...

            Run ``%manim --help`` and ``%manim render --help`` for possible command line interface options.

            .. note::

                The maximal width of the rendered videos that are displayed in the notebook can be
                configured via the ``media_width`` configuration option. The default is set to ``25vw``,
                which is 25% of your current viewport width. To allow the output to become as large
                as possible, set ``config.media_width = "100%"``.

                The ``media_embed`` option will embed the image/video output in the notebook. This is
                generally undesirable as it makes the notebooks very large, but is required on some
                platforms (notably Google's CoLab, where it is automatically enabled unless suppressed
                by ``config.embed = False``) and needed in cases when the notebook (or converted HTML
                file) will be moved relative to the video locations. Use-cases include building
                documentation with Sphinx and JupyterBook. See also the :mod:`manim directive for Sphinx
                <manim.utils.docbuild.manim_directive>`.

            Examples
            --------

            First make sure to put ``import manim``, or even ``from manim import *``
            in a cell and evaluate it. Then, a typical Jupyter notebook cell for Manim
            could look as follows::

                %%manim -v WARNING --disable_caching -qm BannerExample

                config.media_width = "75%"
                config.media_embed = True

                class BannerExample(Scene):
                    def construct(self):
                        self.camera.background_color = "#ece6e2"
                        banner_large = ManimBanner(dark_theme=False).scale(0.7)
                        self.play(banner_large.create())
                        self.play(banner_large.expand())

            Evaluating this cell will render and display the ``BannerExample`` scene defined in the body of the cell.

            .. note::

                In case you want to hide the red box containing the output progress bar, the ``progress_bar`` config
                option should be set to ``None``. This can also be done by passing ``--progress_bar None`` as a
                CLI flag.

            z-hz--helpz	--versionFmanim)standalone_mode	prog_nameNr   r   )OpenGLRendererscene_names)rendereroutput_filezNo output file produced	media_dirjupyterT)parentsexist_okmedia_embedzgoogle.colabimage)filenamez)controls autoplay loop style="max-width: media_widthz;")html_attributesembed)*execsplitlenr
   add_additional_args
isinstancedictr	   getr   digest_argsr0   r   OPENGLmanim.renderer.opengl_rendererr.   renderr   clearwindowcloser   infor   relative_tocwd_generate_file_namesuffixr   unlinkparentmkdirshutilcopy	mimetypes
guess_typer%   r   
startswithr   r   r   )r    r$   r&   r(   argsmodified_argsr0   r.   
SceneClassscene
local_pathtmpfile	file_typer;   results                  r"   r+   zManimMagic.manim'   s   b  %T8$$$::<<Dt99 D0@0@KSWDWDWT5GDDDD 44T::MuPPPDh-----HLL26677 9  9 "4(((?l&999MMMMMM-~//H0!)&*?*B!CJ&J999ELLNNN ).000  +0K --/// ).000  +0K --//// ,0K -(0K 9:::19  9  9  9  9  9  9  9 4 "&"788DDTXZZPP
,-- ,..C
0ACCD  !444'
3::<<<29#J/$$TD$AAAJ000%01FGGJ	!)S11111}- A +c+--.@.@@E''00 "F=,ABBBFF"(mTZ[hTi(m(m(m#  F s9  9  9  9  9  9  9  9  9  9  9  9  9  9  9  9  9  9 s2   7AM?99E(26M?(9F!!%M?FM??NNrW   	list[str]c                \    dg}d|v rd|vr|ddgz  }||d d         z   dgz   |d         gz   S )Nz	--jupyterz-tz--formatwebm  )r    rW   additional_argss      r"   r?   zManimMagic.add_additional_args   sP    *mOt||
$ 6 6J#77"T#2#Y."5b
BBr#   )r   r   r   r   )NN)r$   r%   r&   r'   r(   r)   r   r   )rW   r_   r   r_   )	__name__
__module____qualname__r   r   r   r+   r?   __classcell__)r!   s   @r"   r   r   !   s        	7 	7 	7 	7 	7 	7 
	  $.2	S	  S	  S	  S	  
 
	S	 j	C 	C 	C 	C 	C 	C 	C 	Cr#   r   r%   c                     t           d         d         dz   t          j                                        d          z   } | S )Nr/   r   @z%Y-%m-%d@%H-%M-%S)r   r   nowstrftime)vals    r"   rM   rM      s:    }a 3&)@)@AT)U)UU  Jr#   )r   r%   )%__doc__
__future__r   rT   rR   r   pathlibr   typingr   r+   r   r   r	   manim.__main__r
   manim.renderer.shaderr   	constantsr   __all__IPythonr   IPython.core.interactiveshellr   IPython.core.magicr   r   r   r   IPython.displayr   r   r   r   ImportErrorrM   rd   r#   r"   <module>r|      s   O O " " " " " "                        , , , , , , , , , ,       6 6 6 6 6 6 $ $ $ $ $ $.pC######>>>>>>            6555555555
 aC aC aC aC aCV aC aC \aC aC  	 	 	D	R     s   "A< <BB