freddyaboulton HF staff commited on
Commit
4625bc6
1 Parent(s): 59e613b

Improvement

Browse files
Files changed (1) hide show
  1. index.html +45 -25
index.html CHANGED
@@ -40,7 +40,7 @@
40
 
41
  .grid-container {
42
  display: grid;
43
- grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
44
  grid-gap: 10px;
45
  margin-top: 3.5rem;
46
  margin-left: 3.5rem;
@@ -71,53 +71,62 @@
71
 
72
  Alpine.data("themesData", () => ({
73
  async init() {
74
- const data = await this.getThemes(this.page, this.sort, this.filter);
75
- this.themes = data.pageThemes;
76
- console.log(this.themes);
77
  this.totalPages = data.totalPages;
78
- console.log(this.totalPages);
79
  },
80
  themes: [],
81
  filter: "all",
82
  sort: "likes",
83
  page: 1,
84
  totalPages: -1,
 
85
  buttonClass(attr, filter) {
86
  if (this[attr] === filter) {
87
  return "bg-black dark:bg-white shadow-lg text-white dark:text-black hover:bg-black hover:text-white";
88
  }
89
  return "text-gray-600 dark:text-gray-300 hover:bg-gray-200 dark:hover:bg-gray-500 hover:text-gray-800";
90
  },
91
- async sortModels(sort) {
92
- // this.sort = sort;
93
- // this.page = 1;
94
- // const data = await this.getModels(this.page, this.sort, this.filter);
95
- // this.models = data.models;
96
- // this.totalPages = data.totalPages;
 
 
 
 
 
 
 
97
  },
98
- async getThemes(page, sort, style) {
 
99
  const res = await fetch(
100
- 'https://huggingface.co/datasets/freddyaboulton/gradio-theme-subdomains/resolve/main/subdomains.json'
101
  );
102
  const data = await res.json();
103
- const themes = Object.entries(data).map(([key, value]) => ({
104
- id: key,
105
- subdomain: value,
106
- }))
 
 
107
 
108
- const pageThemes = themes.slice((page - 1) * 30, page * 30);
109
 
110
  return {
111
- pageThemes,
112
- totalPages: Math.floor(themes.length + 30 - 1, 30)
113
  };
114
  },
115
  async nextPage() {
116
  if (this.page < this.totalPages) {
117
  this.page += 1;
118
- const data = await this.getThemes(this.page, this.sort, this.filter);
119
- this.themes = this.themes.concat(data.pageThemes);
120
- // this.totalPages = data.totalPages;
121
  }
122
  },
123
  }));
@@ -139,18 +148,29 @@
139
  <button
140
  :class="buttonClass('sort', 'likes')"
141
  class="text-gray-600 hover:bg-gray-200 dark:hover:bg-gray-500 hover:text-gray-800 px-2 md:px-3 py-1 rounded-full"
142
- @click="sortModels('likes')"
143
  >
144
  Most Likes
145
  </button>
146
  <button
147
  :class="buttonClass('sort', 'recent')"
148
  class="text-gray-600 hover:bg-gray-200 dark:hover:bg-gray-500 hover:text-gray-800 px-2 md:px-3 py-1 rounded-full"
149
- @click="sortModels('recent')"
150
  >
151
  Recent
152
  </button>
153
  </div>
 
 
 
 
 
 
 
 
 
 
 
154
  </div>
155
  </section>
156
  <div class="grid-container">
 
40
 
41
  .grid-container {
42
  display: grid;
43
+ grid-template-columns: repeat(3, minmax(300px, 1fr));
44
  grid-gap: 10px;
45
  margin-top: 3.5rem;
46
  margin-left: 3.5rem;
 
71
 
72
  Alpine.data("themesData", () => ({
73
  async init() {
74
+ const data = await this.getThemes(this.page, this.sort, this.useTestData);
75
+ this.themes = data.themes;
 
76
  this.totalPages = data.totalPages;
 
77
  },
78
  themes: [],
79
  filter: "all",
80
  sort: "likes",
81
  page: 1,
82
  totalPages: -1,
83
+ useTestData: false,
84
  buttonClass(attr, filter) {
85
  if (this[attr] === filter) {
86
  return "bg-black dark:bg-white shadow-lg text-white dark:text-black hover:bg-black hover:text-white";
87
  }
88
  return "text-gray-600 dark:text-gray-300 hover:bg-gray-200 dark:hover:bg-gray-500 hover:text-gray-800";
89
  },
90
+ async switchData() {
91
+ this.page = 1;
92
+ this.useTestData = !this.useTestData;
93
+ const data = await this.getThemes(this.page, this.sort, this.useTestData);
94
+ this.themes = data.themes;
95
+ this.totalPages = data.totalPages;
96
+ },
97
+ async sortThemes(sort) {
98
+ this.sort = sort;
99
+ this.page = 1;
100
+ const data = await this.getThemes(this.page, this.sort, this.useTestData);
101
+ this.themes = data.themes;
102
+ this.totalPages = data.totalPages;
103
  },
104
+ async getThemes(page, sort, useTestData) {
105
+ const filename = useTestData ? "test_data.json" : "subdomains.json"
106
  const res = await fetch(
107
+ `https://huggingface.co/datasets/freddyaboulton/gradio-theme-subdomains/resolve/main/${filename}`
108
  );
109
  const data = await res.json();
110
+ if(sort === 'likes') {
111
+ data.sort((a, b) => (b.likes - a.likes));
112
+ }else {
113
+ data.sort((a, b) => (new Date(b.lastModified) - new Date(a.lastModified)));
114
+ }
115
+ const pageThemes = data.slice((page - 1) * 30, page * 30);
116
 
117
+ console.log(pageThemes);
118
 
119
  return {
120
+ themes: pageThemes,
121
+ totalPages: Math.floor(data.length + 30 - 1, 30)
122
  };
123
  },
124
  async nextPage() {
125
  if (this.page < this.totalPages) {
126
  this.page += 1;
127
+ const data = await this.getThemes(this.page, this.filter, this.useTestData);
128
+ this.themes = this.themes.concat(data.themes);
129
+ this.totalPages = data.totalPages;
130
  }
131
  },
132
  }));
 
148
  <button
149
  :class="buttonClass('sort', 'likes')"
150
  class="text-gray-600 hover:bg-gray-200 dark:hover:bg-gray-500 hover:text-gray-800 px-2 md:px-3 py-1 rounded-full"
151
+ @click="sortThemes('likes')"
152
  >
153
  Most Likes
154
  </button>
155
  <button
156
  :class="buttonClass('sort', 'recent')"
157
  class="text-gray-600 hover:bg-gray-200 dark:hover:bg-gray-500 hover:text-gray-800 px-2 md:px-3 py-1 rounded-full"
158
+ @click="sortThemes('recent')"
159
  >
160
  Recent
161
  </button>
162
  </div>
163
+ <div class="flex gap-0">
164
+ <input type="checkbox" @click="switchData()" class="rounded">
165
+ <span class="md:px-3 py-1 dark:text-white text-gray-400 ml-0">Use Test Data</span>
166
+ <!-- <button
167
+ :class="buttonClass('useTestData', false)"
168
+ class="px-2 md:px-3 py-1 rounded-full"
169
+ @click="switchData()"
170
+ >
171
+ Use Test Data
172
+ </button> -->
173
+ </div>
174
  </div>
175
  </section>
176
  <div class="grid-container">