From 1a318a82aeed1e930e5a722e02c660ccf3c23bad Mon Sep 17 00:00:00 2001
From: Rodrigo Nader
Date: Sun, 25 Jun 2023 17:40:14 -0300
Subject: [PATCH 1/2] Update README.md
---
README.md | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)
diff --git a/README.md b/README.md
index 36ea5a20f..6d995e8c2 100644
--- a/README.md
+++ b/README.md
@@ -13,8 +13,9 @@
+
-
+
From f78088bdb24dbd2c0ef3eb4d6429098618c15a2c Mon Sep 17 00:00:00 2001
From: Gabriel Luiz Freitas Almeida
Date: Tue, 27 Jun 2023 09:44:20 -0300
Subject: [PATCH 2/2] =?UTF-8?q?=F0=9F=94=A7=20refactor(=5F=5Fmain=5F=5F.py?=
=?UTF-8?q?,=20main.py):=20move=20setup=5Fstatic=5Ffiles=20function=20to?=
=?UTF-8?q?=20main.py=20and=20rename=20create=5Fapp=20to=20setup=5Fapp=20?=
=?UTF-8?q?=E2=9C=A8=20feat(=5F=5Fmain=5F=5F.py,=20main.py):=20add=20suppo?=
=?UTF-8?q?rt=20for=20a=20custom=20static=20files=20directory=20to=20be=20?=
=?UTF-8?q?passed=20as=20an=20argument=20to=20the=20app=20The=20`setup=5Fs?=
=?UTF-8?q?tatic=5Ffiles`=20function=20has=20been=20moved=20from=20`=5F=5F?=
=?UTF-8?q?main=5F=5F.py`=20to=20`main.py`=20to=20improve=20code=20organiz?=
=?UTF-8?q?ation.=20The=20function=20has=20also=20been=20renamed=20to=20`s?=
=?UTF-8?q?etup=5Fapp`=20to=20better=20reflect=20its=20purpose.=20The=20`c?=
=?UTF-8?q?reate=5Fapp`=20function=20has=20been=20renamed=20to=20`setup=5F?=
=?UTF-8?q?app`=20to=20follow=20the=20naming=20convention=20of=20the=20new?=
=?UTF-8?q?=20function.=20The=20`setup=5Fapp`=20function=20now=20accepts?=
=?UTF-8?q?=20an=20optional=20argument=20`static=5Ffiles=5Fdir`=20which=20?=
=?UTF-8?q?allows=20the=20user=20to=20specify=20a=20custom=20directory=20f?=
=?UTF-8?q?or=20static=20files.=20This=20improves=20the=20flexibility=20of?=
=?UTF-8?q?=20the=20app=20as=20it=20can=20now=20be=20run=20with=20a=20cust?=
=?UTF-8?q?om=20frontend.?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
src/backend/langflow/__main__.py | 40 ++++----------------------------
src/backend/langflow/main.py | 40 ++++++++++++++++++++++++++++++++
2 files changed, 44 insertions(+), 36 deletions(-)
diff --git a/src/backend/langflow/__main__.py b/src/backend/langflow/__main__.py
index a3841ec93..377339c98 100644
--- a/src/backend/langflow/__main__.py
+++ b/src/backend/langflow/__main__.py
@@ -1,6 +1,5 @@
import sys
import time
-from fastapi import FastAPI
import httpx
from multiprocess import Process, cpu_count # type: ignore
import platform
@@ -11,9 +10,7 @@ from rich.panel import Panel
from rich import box
from rich import print as rprint
import typer
-from fastapi.staticfiles import StaticFiles
-from fastapi.responses import FileResponse
-from langflow.main import create_app
+from langflow.main import setup_app
from langflow.settings import settings
from langflow.utils.logger import configure, logger
import webbrowser
@@ -144,15 +141,9 @@ def serve(
remove_api_keys=remove_api_keys,
cache=cache,
)
- # get the directory of the current file
- if not path:
- frontend_path = Path(__file__).parent
- static_files_dir = frontend_path / "frontend"
- else:
- static_files_dir = Path(path)
-
- app = create_app()
- setup_static_files(app, static_files_dir)
+ # create path object if path is provided
+ path = Path(path) if path else path
+ app = setup_app(static_files_dir=path)
# check if port is being used
if is_port_in_use(port, host):
port = get_free_port(port)
@@ -200,29 +191,6 @@ def run_on_windows(host, port, log_level, options, app):
run_langflow(host, port, log_level, options, app)
-def setup_static_files(app: FastAPI, static_files_dir: Path):
- """
- Setup the static files directory.
-
- Args:
- app (FastAPI): FastAPI app.
- path (str): Path to the static files directory.
- """
- app.mount(
- "/",
- StaticFiles(directory=static_files_dir, html=True),
- name="static",
- )
-
- @app.exception_handler(404)
- async def custom_404_handler(request, __):
- path = static_files_dir / "index.html"
-
- if not path.exists():
- raise RuntimeError(f"File at path {path} does not exist.")
- return FileResponse(path)
-
-
def is_port_in_use(port, host="localhost"):
"""
Check if a port is in use.
diff --git a/src/backend/langflow/main.py b/src/backend/langflow/main.py
index 2a1293f2e..e937931d6 100644
--- a/src/backend/langflow/main.py
+++ b/src/backend/langflow/main.py
@@ -1,5 +1,9 @@
+from pathlib import Path
+from typing import Optional
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
+from fastapi.responses import FileResponse
+from fastapi.staticfiles import StaticFiles
from langflow.api import router
from langflow.database.base import create_db_and_tables
@@ -33,6 +37,42 @@ def create_app():
return app
+def setup_static_files(app: FastAPI, static_files_dir: Path):
+ """
+ Setup the static files directory.
+ Args:
+ app (FastAPI): FastAPI app.
+ path (str): Path to the static files directory.
+ """
+ app.mount(
+ "/",
+ StaticFiles(directory=static_files_dir, html=True),
+ name="static",
+ )
+
+ @app.exception_handler(404)
+ async def custom_404_handler(request, __):
+ path = static_files_dir / "index.html"
+
+ if not path.exists():
+ raise RuntimeError(f"File at path {path} does not exist.")
+ return FileResponse(path)
+
+
+# app = create_app()
+# setup_static_files(app, static_files_dir)
+def setup_app(static_files_dir: Optional[Path]) -> FastAPI:
+ """Setup the FastAPI app."""
+ # get the directory of the current file
+ if not static_files_dir:
+ frontend_path = Path(__file__).parent
+ static_files_dir = frontend_path / "frontend"
+
+ app = create_app()
+ setup_static_files(app, static_files_dir)
+ return app
+
+
app = create_app()