Skip to content

Commit

Permalink
serve HTMLResponse for saved static pages
Browse files Browse the repository at this point in the history
  • Loading branch information
anish-work committed Jul 29, 2024
1 parent 147df74 commit 4fd8608
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 2 deletions.
81 changes: 81 additions & 0 deletions daras_ai_v2/static_pages.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import io
import gooey_ui as st
from static_pages.models import StaticPage
from google.cloud import storage

from bs4 import BeautifulSoup
from daras_ai_v2.settings import GCP_PROJECT, GCS_CREDENTIALS, GS_BUCKET_NAME


client = storage.Client(
GCP_PROJECT,
GCS_CREDENTIALS,
)
bucket = client.get_bucket(GS_BUCKET_NAME)


def populate_imported_css(html: str, uid: str):
soup = BeautifulSoup(html, "html.parser")
links = soup.find_all("link")
hrefList = [link.get("href") for link in links if link.get("href") is not None]
# Remove all <link> tags
for link in links:
link.decompose()

styles = get_all_styles(hrefList, uid)
style_tag = BeautifulSoup(styles, "html.parser").style
# Insert the style tag into the body
if soup.body:
soup.body.insert(0, style_tag)
else:
# If body tag does not exist, create it and add style tag
body_tag = soup.new_tag("body")
body_tag.insert(0, style_tag)
soup.append(body_tag)

return soup


def get_all_styles(links: list, uid: str):
styles = ""
for link in links:
if not link.endswith(".css"): # ignore for css files
continue
blob = bucket.get_blob(f"{uid}/{link}")
blob = blob.download_as_string()
blob = blob.decode("utf-8")
blob = io.StringIO(blob).read()
styles += blob

return f"<style>{styles}</style>"


def serve(page_slug: str, file_path: str = None):
static_page = StaticPage.objects.get(path=page_slug)

if not static_page:
return None

uid = static_page.uid

def render_page():
if file_path:
return None
html = None
blob = bucket.get_blob(f"{uid}/index.html")
blob = blob.download_as_string()
blob = blob.decode("utf-8")
html = io.StringIO(blob).read()
withStyleHtml = populate_imported_css(
html, uid
) # Add all the styles in the html

return withStyleHtml

def get_file_url():
if not file_path:
return None
STATIC_URL = f"https://storage.googleapis.com/gooey-test/{uid}"
return f"{STATIC_URL}/{file_path}"

return render_page(), get_file_url()
23 changes: 21 additions & 2 deletions routers/root.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
PlainTextResponse,
Response,
FileResponse,
HTMLResponse,
)

import gooey_gui as gui
Expand All @@ -43,6 +44,8 @@
from daras_ai_v2.query_params_util import extract_query_params
from daras_ai_v2.settings import templates
from handles.models import Handle
from daras_ai_v2.functional import R
from static_pages.models import StaticPage

app = APIRouter()

Expand Down Expand Up @@ -576,14 +579,30 @@ def chat_lib_route(request: Request, integration_id: str, integration_name: str
"/{page_slug}/",
"/{page_slug}/{run_slug}/",
"/{page_slug}/{run_slug}-{example_id}/",
"/{page_slug}/{path:path}",
)
def recipe_page_or_handle(
request: Request, page_slug: str, run_slug: str = None, example_id: str = None
request: Request,
page_slug: str,
run_slug: str = None,
example_id: str = None,
path: str = None,
):
try:
handle = Handle.objects.get_by_name(page_slug)
except Handle.DoesNotExist:
return render_page(request, page_slug, RecipeTabs.run, example_id)
try:
import daras_ai_v2.static_pages as static_pages

html, file_path = static_pages.serve(page_slug=page_slug, file_path=path)

if file_path:
return RedirectResponse(file_path)
elif html:
return HTMLResponse(html)
except StaticPage.DoesNotExist:
return render_page(request, page_slug, RecipeTabs.run, example_id)

else:
return render_page_for_handle(request, handle)

Expand Down

0 comments on commit 4fd8608

Please sign in to comment.