Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Build frontend as part of backend build #1224

Open
dokterbob opened this issue Aug 16, 2024 · 1 comment
Open

Build frontend as part of backend build #1224

dokterbob opened this issue Aug 16, 2024 · 1 comment
Labels
architecture Overall project architecture. needs-triage

Comments

@dokterbob
Copy link
Collaborator

Is your feature request related to a problem?
An increasing amount of users are using mount_chainlit() to deploy from within another FastAPI app.

Some of them are running into issues with dependencies (#731 #950) and many of them (including the undersigned) want to make improvements or changes and test them locally.

As it stands, the Python package can be installed from a subdirectory (e.g. poetry add git+https://github.com/myorg/mypackage_with_subdirs.git@main#subdirectory=subdir) but in this case it seems that the frontend is not distributed alongside the package as it would be for PyPI.

Describe the solution you'd like
Ideally, I would suggest that the Python install automatically builds the frontend on install. Alternatively, we should at least have instructions on how to proceed in such a case.

Approaches

Build on install

It's not evidently clear how to call arbitrary code (like pnpm run buildUi to build the frontend) during the Python package installation process, especially not when installing as editable/from source.

Example: https://sam.hooke.me/note/2023/08/poetry-build-py-example/

Build on first launch

One approach might be to build the frontend on first application launch.

We'd check for existence of the frontend dir and if not, we build.

Package frontend separately

Another could be to package the frontend as a separate (binary/blob) type of dependency, so that it can be separately installed.

An example of this: https://pypi.org/project/vite-project/ (source: https://github.com/itayB/vite-project)

Resources/references
https://discuss.python.org/t/running-custom-code-on-package-install/16160/2

@dokterbob dokterbob changed the title Facilitate/document deploying as dependency from fork Build frontend as part of backend build Aug 27, 2024
@dokterbob
Copy link
Collaborator Author

Based on my research, we should be able to do this with a builds script, as follows (untested):

pyproject.toml:

[build-system]
requires = ["poetry-core", "setuptools"]

[tool.poetry.build]
script = "scripts/build_frontend.py"
generate-setup-file = true

build_frontend.py:

import subprocess
import sys

def build_frontend():
    try:
        subprocess.run(["npm", "install"], cwd="src/your_package/frontend", check=True)
        subprocess.run(["npm", "run", "build"], cwd="src/your_package/frontend", check=True)
    except subprocess.CalledProcessError:
        print("Error: Failed to build frontend. Make sure Node.js and npm are installed.")
        sys.exit(1)
    except FileNotFoundError:
        print("Error: Node.js or npm not found. Please install Node.js to build the frontend.")
        sys.exit(1)

if __name__ == "__main__":
    build_frontend()

Reference: python-poetry/poetry#2740 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
architecture Overall project architecture. needs-triage
Projects
None yet
Development

No branches or pull requests

1 participant