REF: Remove side-effects from importing Styler
State: closed

- [ ] closes #xxxx (Replace xxxx with the GitHub issue number)
- [ ] [Tests added and passed](https://pandas.pydata.org/pandas-docs/dev/development/contributing_codebase.html#writing-tests) if fixing a bug or adding a new feature
- [ ] All [code checks passed](https://pandas.pydata.org/pandas-docs/dev/development/contributing_codebase.html#pre-commit).
- [ ] Added [type annotations](https://pandas.pydata.org/pandas-docs/dev/development/contributing_codebase.html#type-hints) to new arguments/methods/functions.
- [ ] Added an entry in the latest `doc/source/whatsnew/vX.X.X.rst` file if fixing a bug or adding a new feature.

I initially pursued this because I thought it was necessary for #27522, but realize now Styler isn't defined in core. However it would be necessary for #52994 if we decide to go that route.

The "cached classmethods" are a bit of a hack. It works because when you set e.g. `StylerRenderer.template_string = some_value`, you're just overwriting the entire property with the value. Still looking for a better way to do this.

Labels:
name: Refactor
description: Internal refactoring of code
name: Dependencies
description: Required and optional dependencies
name: Styler
description: conditional formatting using DataFrame.style
name: Stale

Comments:
What about pushing the loading of templates into the render methods themselves. The class level parameters could just be strings defining the default template file location? Maybe keep the cached env loader.

Currently we have

```python
def _render_html(...) -> str:
        d = self._render(sparse_index, sparse_columns, max_rows, max_cols, "&nbsp;")
        d.update(kwargs)
        return self.template_html.render(
            **d,
            html_table_tpl=self.template_html_table,
            html_style_tpl=self.template_html_style, 
        )
```

then perhaps ..

```python
class StyleRender:
      template_html = "some_string_path"
      ...


def _render_html(...) -> str:
        d = self._render(sparse_index, sparse_columns, max_rows, max_cols, "&nbsp;")
        d.update(kwargs)
        template_html = self.env.get_template(self.template_html)
        template_html_table = self.env.get_template(self.template_html_table)
        template_html_style = self.env.get_template(self.template_html_style)
        return template_html.render(
            **d,
            html_table_tpl=template_html_table, 
            html_style_tpl=template_html_style, 
        )
```
@attack68: finally had some time to look into this. Your suggestion would make it so that users can no longer modify the templates. Is that a breaking change?
Creating modified templates is documented as:

```python
class MyStyler(Styler):
    env = Environment(
        loader=ChoiceLoader([
            FileSystemLoader("templates"),  # contains ours
            Styler.loader,  # the default
        ])
    )
    template_html_table = env.get_template("myhtml.tpl")
```
where `myhtml.tpl` is a custom template file in a user directory.

It is my understanding that the native template files stored in the python environment are not really modifiable or suitable to be modified, but a user has to add custom templates which might refer to them, as is the case in the documentation.

Im sure it is possible to create a more user friendly way of doing this which can still avoid your caching.

> Im sure it is possible to create a more user friendly way of doing this which can still avoid your caching.

Are you interested in taking this up?
consider #53429
This pull request is stale because it has been open for thirty days with no activity. Please [update](https://pandas.pydata.org/pandas-docs/stable/development/contributing.html#updating-your-pull-request) and respond to this comment if you're still interested in working on this.