Ramesh-vani commited on
Commit
036879b
·
verified ·
1 Parent(s): 233a7ce

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -52
app.py CHANGED
@@ -923,58 +923,43 @@ async def exe(websocket,connected,key,process_ids):
923
  else:
924
  response = {"error": "Unsupported method"}
925
  # Parse HTML content
926
- soup = BeautifulSoup(response.text, "html.parser")
927
- # Get base URL
928
- base_url = url
929
- # base_host = base_url.netloc.rstrip("/")
930
-
931
- # Function to join relative URLs with base URL
932
- def join_url_with_base(url_arg):
933
- if url_arg.startswith('/'):
934
- # print(url+url_arg)
935
- return url+url_arg
936
- elif url_arg.startswith('http'):
937
- # print(here)
938
- # print(url_arg)
939
- return url_arg
940
- else:
941
- # print(url_arg)
942
- return url+url_arg
943
-
944
- # Find and modify CSS links
945
- for link in soup.find_all("link", rel="stylesheet", href=True):
946
- link['href'] = join_url_with_base(link['href'])
947
-
948
- # Find and modify JS links
949
- for script in soup.find_all("script", src=True):
950
- script['src'] = join_url_with_base(script['src'])
951
-
952
- # Find and modify regular anchor links
953
- for anchor in soup.find_all("a", href=True):
954
- anchor['href'] = join_url_with_base(anchor['href'])
955
- # Find and modify CSS links
956
- for link in soup.find_all("link", rel="stylesheet", href=True):
957
- if link['href'].startswith('/'):
958
- css_url = url + link['href']
959
- css_response = requests.get(css_url)
960
- css_content = css_response.text
961
- style_tag = soup.new_tag("style")
962
- style_tag.string = css_content
963
- link.replace_with(style_tag)
964
-
965
- # Find and modify JS links
966
- for script in soup.find_all("script", src=True):
967
- if script['src'].startswith('/'):
968
- js_url = url + script['src']
969
- js_response = requests.get(js_url)
970
- js_content = js_response.text
971
- script_tag = soup.new_tag("script")
972
- script_tag.string = js_content
973
- script.replace_with(script_tag)
974
- response_data = {
975
- "type": "web-data",
976
- "data": str(soup),
977
- }
978
  await websocket.send(json.dumps(response_data))
979
  elif event["command"]["type"]=="create":
980
  if event["item"]=="folder":
 
923
  else:
924
  response = {"error": "Unsupported method"}
925
  # Parse HTML content
926
+ souptmp = BeautifulSoup(response.text, "html.parser")
927
+ # Step 1: Define the initial HTML string
928
+ html_str = """
929
+ <!DOCTYPE html>
930
+ <html lang="en">
931
+ <head>
932
+ <meta charset="UTF-8">
933
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
934
+ <title>Sample HTML</title>
935
+ </head>
936
+ <body>
937
+ <h1>Hello, World!</h1>
938
+ </body>
939
+ </html>
940
+ """
941
+
942
+ # Step 2: Parse the initial HTML content
943
+ soup = BeautifulSoup(html_str, 'html.parser')
944
+
945
+ # Step 3: Define the new HTML snippet to replace the body content
946
+ new_body_content = souptmp
947
+
948
+ # Step 4: Parse the new HTML snippet
949
+ new_body = BeautifulSoup(new_body_content, 'html.parser')
950
+
951
+ # Step 5: Replace the existing body content with the new content
952
+ if soup.body:
953
+ soup.body.clear()
954
+ soup.body.append(new_body)
955
+ else:
956
+ print("No <body> tag found in the HTML")
957
+ # Inline CSS and JavaScript
958
+ soup = inline_resources(soup, url)
959
+ response_data = {
960
+ "type": "web-data",
961
+ "data": str(soup),
962
+ }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
963
  await websocket.send(json.dumps(response_data))
964
  elif event["command"]["type"]=="create":
965
  if event["item"]=="folder":