rwillats commited on
Commit
06545b7
·
verified ·
1 Parent(s): f760d0f

Upload folder using huggingface_hub

Browse files
Files changed (1) hide show
  1. test.py +23 -41
test.py CHANGED
@@ -3,9 +3,6 @@ import base64
3
  import os
4
 
5
  def create_pdf_popup_app():
6
- """
7
- Creates a Gradio app with a button that shows a PDF in a modal popup.
8
- """
9
  def get_pdf_base64():
10
  pdf_path = "Hate Speech Policy.pdf"
11
  if not os.path.exists(pdf_path):
@@ -20,9 +17,9 @@ def create_pdf_popup_app():
20
  except Exception as e:
21
  print(f"Error reading PDF file: {str(e)}")
22
  return None
23
-
24
  pdf_base64 = get_pdf_base64()
25
-
26
  modal_css = """
27
  .pdf-modal {
28
  display: none;
@@ -81,15 +78,14 @@ def create_pdf_popup_app():
81
  font-size: 18px;
82
  }
83
  """
84
-
85
  if pdf_base64:
86
  modal_html = f"""
87
  <div id="pdfModal" class="pdf-modal">
88
  <div class="pdf-modal-content">
89
- <button class="close-btn" onclick="document.getElementById('pdfModal').style.display='none';">×</button>
90
  <div class="pdf-container">
91
- <iframe class="pdf-embed" src="data:application/pdf;base64,{pdf_base64}#toolbar=0&navpanes=0"
92
- type="application/pdf" frameborder="0"></iframe>
93
  </div>
94
  </div>
95
  </div>
@@ -98,53 +94,39 @@ def create_pdf_popup_app():
98
  modal_html = """
99
  <div id="pdfModal" class="pdf-modal">
100
  <div class="pdf-modal-content">
101
- <button class="close-btn" onclick="document.getElementById('pdfModal').style.display='none';">×</button>
102
  <div class="pdf-container">
103
- <p class="pdf-error">
104
- Error: Could not load the PDF file. Please make sure "Hate Speech Policy.pdf" exists in the same directory.
105
- </p>
106
  </div>
107
  </div>
108
  </div>
109
  """
110
-
111
  with gr.Blocks(css=modal_css) as app:
112
  gr.Markdown("# PDF Modal Popup Demo")
113
  gr.HTML(modal_html)
114
-
115
- view_button = gr.Button("View Hate Speech Policy")
116
-
117
- # More reliable way to attach the click handler
118
- view_button.click(
119
- fn=None,
120
- inputs=None,
121
- outputs=None,
122
- _js="""
123
- function() {
124
- const modal = document.getElementById('pdfModal');
125
- if (modal) {
126
- modal.style.display = 'block';
127
- // Prevent body scrolling when modal is open
128
- document.body.style.overflow = 'hidden';
129
- }
130
- return [];
131
- }
132
- """
133
- )
134
-
135
- # Add script to handle modal closing and body scroll
136
  gr.HTML("""
137
  <script>
138
- document.addEventListener('click', function(e) {
 
139
  const modal = document.getElementById('pdfModal');
140
- if (modal && e.target === modal) {
141
- modal.style.display = 'none';
142
- document.body.style.overflow = 'auto';
 
 
 
 
 
 
 
 
143
  }
144
  });
145
  </script>
146
  """)
147
-
148
  return app
149
 
150
  if __name__ == "__main__":
 
3
  import os
4
 
5
  def create_pdf_popup_app():
 
 
 
6
  def get_pdf_base64():
7
  pdf_path = "Hate Speech Policy.pdf"
8
  if not os.path.exists(pdf_path):
 
17
  except Exception as e:
18
  print(f"Error reading PDF file: {str(e)}")
19
  return None
20
+
21
  pdf_base64 = get_pdf_base64()
22
+
23
  modal_css = """
24
  .pdf-modal {
25
  display: none;
 
78
  font-size: 18px;
79
  }
80
  """
81
+
82
  if pdf_base64:
83
  modal_html = f"""
84
  <div id="pdfModal" class="pdf-modal">
85
  <div class="pdf-modal-content">
86
+ <button class="close-btn" onclick="document.getElementById('pdfModal').style.display='none';document.body.style.overflow='auto';">×</button>
87
  <div class="pdf-container">
88
+ <iframe class="pdf-embed" src="data:application/pdf;base64,{pdf_base64}#toolbar=0&navpanes=0" type="application/pdf"></iframe>
 
89
  </div>
90
  </div>
91
  </div>
 
94
  modal_html = """
95
  <div id="pdfModal" class="pdf-modal">
96
  <div class="pdf-modal-content">
97
+ <button class="close-btn" onclick="document.getElementById('pdfModal').style.display='none';document.body.style.overflow='auto';">×</button>
98
  <div class="pdf-container">
99
+ <p class="pdf-error">Error: Could not load the PDF file.</p>
 
 
100
  </div>
101
  </div>
102
  </div>
103
  """
104
+
105
  with gr.Blocks(css=modal_css) as app:
106
  gr.Markdown("# PDF Modal Popup Demo")
107
  gr.HTML(modal_html)
108
+ gr.Button("View Hate Speech Policy", elem_id="open-modal-button")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
109
  gr.HTML("""
110
  <script>
111
+ document.addEventListener('DOMContentLoaded', function () {
112
+ const button = document.getElementById('open-modal-button');
113
  const modal = document.getElementById('pdfModal');
114
+ if (button && modal) {
115
+ button.addEventListener('click', () => {
116
+ modal.style.display = 'block';
117
+ document.body.style.overflow = 'hidden';
118
+ });
119
+ window.addEventListener('click', function(e) {
120
+ if (e.target === modal) {
121
+ modal.style.display = 'none';
122
+ document.body.style.overflow = 'auto';
123
+ }
124
+ });
125
  }
126
  });
127
  </script>
128
  """)
129
+
130
  return app
131
 
132
  if __name__ == "__main__":