bluenevus commited on
Commit
a948e2e
·
verified ·
1 Parent(s): 35d836c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +75 -16
app.py CHANGED
@@ -1,4 +1,8 @@
1
- import gradio as gr
 
 
 
 
2
  import requests
3
  from bs4 import BeautifulSoup
4
  from urllib.parse import urljoin, urlparse
@@ -13,7 +17,13 @@ import sqlite3
13
  from contextlib import contextmanager
14
  from threading import local
15
  import time
 
16
 
 
 
 
 
 
17
  logging.basicConfig(level=logging.INFO)
18
  logger = logging.getLogger(__name__)
19
 
@@ -166,19 +176,68 @@ async def process_url(url, depth):
166
  logger.error(f"Error in process_url: {str(e)}")
167
  return f"An error occurred: {str(e)}"
168
 
169
- def run_async(url, depth):
170
- return asyncio.run(process_url(url, depth))
171
-
172
- iface = gr.Interface(
173
- fn=run_async,
174
- inputs=[
175
- gr.Textbox(label="Enter website URL (e.g., https://www.gradio.app/docs)"),
176
- gr.Slider(minimum=1, maximum=10, value=3, step=1, label="Crawl Depth")
177
- ],
178
- outputs=gr.File(label="Download PDF"),
179
- title="Website to PDF Converter",
180
- description="Enter docs URL and crawl depth to convert documentation pages into a PDF. Be responsible for sites you have permission to do this"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
181
  )
182
-
183
- if __name__ == "__main__":
184
- iface.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import dash
2
+ from dash import dcc, html, Input, Output, State
3
+ import dash_bootstrap_components as dbc
4
+ from dash.exceptions import PreventUpdate
5
+ import base64
6
  import requests
7
  from bs4 import BeautifulSoup
8
  from urllib.parse import urljoin, urlparse
 
17
  from contextlib import contextmanager
18
  from threading import local
19
  import time
20
+ import os
21
 
22
+ # Initialize Dash app
23
+ app = dash.Dash(__name__, external_stylesheets=[dbc.themes.BOOTSTRAP])
24
+ server = app.server
25
+
26
+ # Logging setup
27
  logging.basicConfig(level=logging.INFO)
28
  logger = logging.getLogger(__name__)
29
 
 
176
  logger.error(f"Error in process_url: {str(e)}")
177
  return f"An error occurred: {str(e)}"
178
 
179
+ # App layout
180
+ app.layout = dbc.Container([
181
+ dbc.Navbar(
182
+ dbc.Container([
183
+ html.A(
184
+ dbc.Row([
185
+ dbc.Col(html.Img(src="/assets/logo.png", height="30px")),
186
+ dbc.Col(dbc.NavbarBrand("Website to PDF Converter", className="ms-2")),
187
+ ],
188
+ align="center",
189
+ className="g-0",
190
+ ),
191
+ href="/",
192
+ style={"textDecoration": "none"},
193
+ )
194
+ ]),
195
+ color="#116F70",
196
+ dark=True,
197
+ ),
198
+
199
+ dbc.Card(
200
+ dbc.CardBody([
201
+ html.H1("Website to PDF Converter", className="text-center mb-4"),
202
+ html.P("Enter docs URL and crawl depth to convert documentation pages into a PDF. Be responsible for sites you have permission to do this", className="text-center mb-4"),
203
+ dbc.Input(id="url-input", type="text", placeholder="Enter website URL (e.g., https://www.gradio.app/docs)", className="mb-3"),
204
+ dcc.Slider(id="depth-slider", min=1, max=10, step=1, value=3, marks={i: str(i) for i in range(1, 11)}, className="mb-3"),
205
+ dbc.Button("Convert to PDF", id="submit-button", color="primary", className="mb-3 w-100"),
206
+ dbc.Spinner(html.Div(id="output-area"), color="primary", type="grow"),
207
+ ]),
208
+ className="mt-4"
209
+ )
210
+ ], fluid=True)
211
+
212
+ @app.callback(
213
+ Output("output-area", "children"),
214
+ Input("submit-button", "n_clicks"),
215
+ State("url-input", "value"),
216
+ State("depth-slider", "value"),
217
+ prevent_initial_call=True
218
  )
219
+ def update_output(n_clicks, url, depth):
220
+ if not url:
221
+ return "Please enter a valid URL."
222
+
223
+ pdf_path = asyncio.run(process_url(url, depth))
224
+
225
+ if pdf_path.startswith("An error occurred"):
226
+ return pdf_path
227
+
228
+ with open(pdf_path, "rb") as f:
229
+ encoded = base64.b64encode(f.read()).decode()
230
+
231
+ os.unlink(pdf_path) # Remove the temporary file
232
+
233
+ return html.Div([
234
+ html.H4("PDF Generated Successfully"),
235
+ html.A(
236
+ dbc.Button("Download PDF", color="success", className="mt-2"),
237
+ href=f"data:application/pdf;base64,{encoded}",
238
+ download="website_content.pdf"
239
+ )
240
+ ])
241
+
242
+ if __name__ == '__main__':
243
+ app.run_server(debug=True, host='0.0.0.0', port=7860)