Skip to content

Commit

Permalink
Added mechanism to allow injecting custom CSS files for UI customizat…
Browse files Browse the repository at this point in the history
…ion (Chainlit#373)

* Added mechanism to allow injecting custom CSS files for UI customization

* Removed parenthesis.
  • Loading branch information
onepointconsulting authored Sep 7, 2023
1 parent edc1bb8 commit 7fee8e0
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 0 deletions.
2 changes: 2 additions & 0 deletions backend/chainlit/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,8 @@ class UISettings(DataClassJsonMixin):
default_expand_messages: bool = False
github: Optional[str] = None
theme: Optional[Theme] = None
# Optional custom CSS file that allows you to customize the UI
custom_css: Optional[str] = None


@dataclass()
Expand Down
7 changes: 7 additions & 0 deletions backend/chainlit/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ def get_build_dir():
def get_html_template():
PLACEHOLDER = "<!-- TAG INJECTION PLACEHOLDER -->"
JS_PLACEHOLDER = "<!-- JS INJECTION PLACEHOLDER -->"
CSS_PLACEHOLDER = "<!-- CSS INJECTION PLACEHOLDER -->"

default_url = "https://github.com/Chainlit/chainlit"
url = config.ui.github or default_url
Expand All @@ -193,13 +194,19 @@ def get_html_template():
if config.ui.theme:
js = f"""<script>window.theme = {json.dumps(config.ui.theme.to_dict())}</script>"""

css = None
if config.ui.custom_css:
css = f"""<link rel="stylesheet" type="text/css" href="{config.ui.custom_css}">"""

index_html_file_path = os.path.join(build_dir, "index.html")

with open(index_html_file_path, "r", encoding="utf-8") as f:
content = f.read()
content = content.replace(PLACEHOLDER, tags)
if js:
content = content.replace(JS_PLACEHOLDER, js)
if css:
content = content.replace(CSS_PLACEHOLDER, css)
return content


Expand Down
1 change: 1 addition & 0 deletions frontend/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
rel="stylesheet"
/>
<!-- JS INJECTION PLACEHOLDER -->
<!-- CSS INJECTION PLACEHOLDER -->
<script>
const global = globalThis;
</script>
Expand Down

0 comments on commit 7fee8e0

Please sign in to comment.