Illia56 commited on
Commit
1afeb4a
1 Parent(s): 174beab

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +95 -11
app.py CHANGED
@@ -90,15 +90,17 @@ def display_channel_statistics(statistics):
90
  return statistics["error"]
91
  return f"""
92
  <div style='text-align: center;'>
93
- <img src='{statistics["Cover"]}' style='width: 100%; max-width: 300px; height: auto; text-align: center;'><br>
94
- <a href='{statistics["URL"]}'>{statistics["Title"]}</a><br>
95
- Subscribers: {statistics["Subscribers"]}<br>
96
- Views: {statistics["Views"]}<br>
97
- Total videos: {statistics["Total videos"]}
 
 
98
  </div>
99
  """
100
 
101
- # Format and display video data as HTML table
102
  def display_video_data(video_data):
103
  if "error" in video_data:
104
  return video_data["error"]
@@ -107,10 +109,50 @@ def display_video_data(video_data):
107
  video_data['Video'] = video_data.apply(
108
  lambda row: f'<a href="{row["Video"]}" target="_blank">{row["Title"]}</a><br>{row["PublishedAt"]}', axis=1
109
  )
110
- video_data['Thumbnail'] = video_data['Thumbnail'].apply(lambda x: f'<img src="{x}" style="width: 100%; max-width: 100px; height: auto;"><br>')
111
  video_data['Video'] = video_data['Thumbnail'] + video_data['Video']
112
  video_data = video_data.drop(['Thumbnail', 'Title', 'PublishedAt'], axis=1)
113
- return video_data.to_html(escape=False, index=False).replace('text-align: right;', 'text-align: center;')
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
114
 
115
  # Load API key from environment variable
116
  api_key = os.getenv('API_KEY')
@@ -131,18 +173,60 @@ def fetch_data(selected_category, platform):
131
  return statistics_display, video_data_display
132
 
133
  # Define and launch Gradio interface
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
134
  gr_interface = gr.Interface(
135
  fn=fetch_data,
136
  inputs=[
137
- gr.Dropdown(choices=list(scheme.keys()), label="Select Channel"),
138
- gr.Dropdown(choices=list(next(iter(scheme.values())).keys()), label="Select Platform")
139
  ],
140
  outputs=[
141
  gr.HTML(label="General Channel Statistics"),
142
  gr.HTML(label="Video Data")
143
  ],
144
  title="Social Media Analytics",
145
- description="Select a channel and platform to fetch analytics data."
 
146
  )
147
 
148
  gr_interface.launch()
 
90
  return statistics["error"]
91
  return f"""
92
  <div style='text-align: center;'>
93
+ <img src='{statistics["Cover"]}' style='width: 100%; max-width: 300px; height: auto; margin-bottom: 15px;'><br>
94
+ <a href='{statistics["URL"]}' style='font-size: 1.5em; font-weight: bold; color: #2c3e50;'>{statistics["Title"]}</a><br>
95
+ <div style='font-size: 1.1em; color: #7f8c8d;'>
96
+ Subscribers: {statistics["Subscribers"]}<br>
97
+ Views: {statistics["Views"]}<br>
98
+ Total videos: {statistics["Total videos"]}
99
+ </div>
100
  </div>
101
  """
102
 
103
+ # Format and display video data as HTML table with enhanced CSS
104
  def display_video_data(video_data):
105
  if "error" in video_data:
106
  return video_data["error"]
 
109
  video_data['Video'] = video_data.apply(
110
  lambda row: f'<a href="{row["Video"]}" target="_blank">{row["Title"]}</a><br>{row["PublishedAt"]}', axis=1
111
  )
112
+ video_data['Thumbnail'] = video_data['Thumbnail'].apply(lambda x: f'<img src="{x}" style="width: 100%; max-width: 100px; height: auto; margin: 5px 0;"><br>')
113
  video_data['Video'] = video_data['Thumbnail'] + video_data['Video']
114
  video_data = video_data.drop(['Thumbnail', 'Title', 'PublishedAt'], axis=1)
115
+
116
+ # Custom CSS for table styling
117
+ custom_css = """
118
+ <style>
119
+ table {
120
+ width: 100%;
121
+ border-collapse: collapse;
122
+ margin: 25px 0;
123
+ font-size: 1em;
124
+ font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
125
+ box-shadow: 0 0 20px rgba(0, 0, 0, 0.05);
126
+ border-radius: 10px;
127
+ overflow: hidden;
128
+ }
129
+ th, td {
130
+ padding: 15px;
131
+ background-color: #ffffff;
132
+ color: #333333;
133
+ }
134
+ th {
135
+ background-color: #f8f9fa;
136
+ font-weight: bold;
137
+ text-align: center;
138
+ border-bottom: 2px solid #e9ecef;
139
+ }
140
+ tr {
141
+ border-bottom: 1px solid #e9ecef;
142
+ }
143
+ tr:nth-of-type(even) {
144
+ background-color: #f8f9fa;
145
+ }
146
+ tr:last-of-type {
147
+ border-bottom: none;
148
+ }
149
+ tr:hover {
150
+ background-color: #f1f1f1;
151
+ }
152
+ </style>
153
+ """
154
+
155
+ return custom_css + video_data.to_html(escape=False, index=False).replace('text-align: right;', 'text-align: center;')
156
 
157
  # Load API key from environment variable
158
  api_key = os.getenv('API_KEY')
 
173
  return statistics_display, video_data_display
174
 
175
  # Define and launch Gradio interface
176
+ css = """
177
+ <style>
178
+ @media (max-width: 600px) {
179
+ .fixed-top {
180
+ position: fixed;
181
+ top: 0;
182
+ width: 100%;
183
+ z-index: 999;
184
+ background: white;
185
+ padding: 10px 0;
186
+ box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
187
+ }
188
+ body {
189
+ padding-top: 70px; /* Adjust based on the height of the fixed element */
190
+ }
191
+ }
192
+ body {
193
+ font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
194
+ background-color: #f3f4f6;
195
+ color: #333333;
196
+ }
197
+ .gradio-container {
198
+ padding: 20px;
199
+ }
200
+ .gr-inputs {
201
+ display: flex;
202
+ justify-content: center;
203
+ margin-bottom: 20px;
204
+ }
205
+ .gr-inputs > div {
206
+ margin: 0 10px;
207
+ }
208
+ .gr-output {
209
+ background: white;
210
+ border-radius: 10px;
211
+ padding: 20px;
212
+ box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
213
+ }
214
+ </style>
215
+ """
216
+
217
  gr_interface = gr.Interface(
218
  fn=fetch_data,
219
  inputs=[
220
+ gr.Dropdown(choices=list(scheme.keys()), label="Select Channel", elem_classes=["fixed-top"]),
221
+ gr.Dropdown(choices=list(next(iter(scheme.values())).keys()), label="Select Platform", elem_classes=["fixed-top"])
222
  ],
223
  outputs=[
224
  gr.HTML(label="General Channel Statistics"),
225
  gr.HTML(label="Video Data")
226
  ],
227
  title="Social Media Analytics",
228
+ description="Select a channel and platform to fetch analytics data.",
229
+ css=css
230
  )
231
 
232
  gr_interface.launch()