soiz1 commited on
Commit
d2f22ed
·
verified ·
1 Parent(s): 9ce5dd9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -41
app.py CHANGED
@@ -159,14 +159,6 @@ def index():
159
  </div>
160
  <button type="submit">Process Image</button>
161
  </form>
162
-
163
- <div id="apiUsage">
164
- <h3>API Usage:</h3>
165
- <p>Here's how to call the API with the current form data:</p>
166
- <div id="formDataPreview">Fill out the form to see the API call code...</div>
167
- <div id="fetchCode"></div>
168
- </div>
169
-
170
  <div id="result">
171
  <h3>Result:</h3>
172
  <div id="outputContainer" style="display: none;">
@@ -174,58 +166,57 @@ def index():
174
  <a id="downloadLink" href="#" download>Download Image</a>
175
  </div>
176
  </div>
 
 
 
 
 
 
177
  </div>
178
 
179
  <script>
180
  // フォームの変更を監視してAPI使用例を更新
181
  function updateApiUsage() {
182
- const form = document.getElementById('uploadForm');
183
  const fileInput = document.getElementById('file');
184
  const version = document.getElementById('version').value;
185
  const scale = document.getElementById('scale').value;
186
 
187
- // フォームデータのJSONを作成
188
- const formJson = {
189
- version: version,
190
- scale: parseFloat(scale)
191
- };
192
 
 
 
193
  if (fileInput.files.length > 0) {
194
  const file = fileInput.files[0];
195
- formJson.file = {
196
- name: file.name,
197
- type: file.type,
198
- size: file.size + ' bytes'
 
 
 
 
 
199
  };
 
200
  } else {
201
- formJson.file = 'No file selected';
202
  }
203
-
204
- updateFormDataPreview(formJson);
205
- updateFetchCode();
206
- }
207
-
208
- function updateFormDataPreview(formJson) {
209
- const previewDiv = document.getElementById('formDataPreview');
210
- previewDiv.innerHTML = '<strong>Form data (JSON):</strong><br>' +
211
- JSON.stringify(formJson, null, 2);
212
  }
213
 
214
- function updateFetchCode() {
215
- // 現在のURLからベースURLを取得(パス、パラメータ、ハッシュを含めない)
216
- const baseUrl = window.location.origin;
217
- const apiUrl = baseUrl + '/api/restore';
218
-
219
  const fetchCodeDiv = document.getElementById('fetchCode');
220
  fetchCodeDiv.innerHTML = `
221
- <div class="code-block">
222
- // JavaScript fetch code:
 
 
 
 
223
  fetch('${apiUrl}', {
224
  method: 'POST',
225
- headers: {
226
- 'Content-Type': 'multipart/form-data'
227
- },
228
- body: ${JSON.stringify(formJson, null, 2)}
229
  })
230
  .then(response => {
231
  if (!response.ok) {
@@ -237,11 +228,11 @@ fetch('${apiUrl}', {
237
  // Process the returned image blob
238
  const url = URL.createObjectURL(blob);
239
  console.log('Image processed successfully', url);
 
240
  })
241
  .catch(error => {
242
  console.error('Error:', error.message);
243
- });
244
- </div>`;
245
  }
246
 
247
  // フォーム要素の変更を監視
 
159
  </div>
160
  <button type="submit">Process Image</button>
161
  </form>
 
 
 
 
 
 
 
 
162
  <div id="result">
163
  <h3>Result:</h3>
164
  <div id="outputContainer" style="display: none;">
 
166
  <a id="downloadLink" href="#" download>Download Image</a>
167
  </div>
168
  </div>
169
+ <div id="apiUsage">
170
+ <h3>API Usage:</h3>
171
+ <div id="fetchCode" class="code-block">
172
+ // JavaScript fetch code will appear here
173
+ </div>
174
+ </div>
175
  </div>
176
 
177
  <script>
178
  // フォームの変更を監視してAPI使用例を更新
179
  function updateApiUsage() {
 
180
  const fileInput = document.getElementById('file');
181
  const version = document.getElementById('version').value;
182
  const scale = document.getElementById('scale').value;
183
 
184
+ // 現在のURLからベースURLを取得(パス、パラメータ、ハッシュを含めない)
185
+ const baseUrl = window.location.origin;
186
+ const apiUrl = baseUrl + '/api/restore';
 
 
187
 
188
+ // ファイルのプレビュー用文字列を準備
189
+ let filePreview = '"img-dataURL"';
190
  if (fileInput.files.length > 0) {
191
  const file = fileInput.files[0];
192
+ const reader = new FileReader();
193
+ reader.onload = function(e) {
194
+ const dataURL = e.target.result;
195
+ if (dataURL.length > 20) {
196
+ filePreview = `"${dataURL.substring(0, 10)}...${dataURL.substring(dataURL.length - 10)}"`;
197
+ } else {
198
+ filePreview = `"${dataURL}"`;
199
+ }
200
+ updateFetchCode(apiUrl, version, scale, filePreview);
201
  };
202
+ reader.readAsDataURL(file);
203
  } else {
204
+ updateFetchCode(apiUrl, version, scale, filePreview);
205
  }
 
 
 
 
 
 
 
 
 
206
  }
207
 
208
+ function updateFetchCode(apiUrl, version, scale, filePreview) {
 
 
 
 
209
  const fetchCodeDiv = document.getElementById('fetchCode');
210
  fetchCodeDiv.innerHTML = `
211
+ // JavaScript fetch example:
212
+ const formData = new FormData();
213
+ formData.append('file', ${filePreview});
214
+ formData.append('version', '${version}');
215
+ formData.append('scale', ${scale});
216
+
217
  fetch('${apiUrl}', {
218
  method: 'POST',
219
+ body: formData
 
 
 
220
  })
221
  .then(response => {
222
  if (!response.ok) {
 
228
  // Process the returned image blob
229
  const url = URL.createObjectURL(blob);
230
  console.log('Image processed successfully', url);
231
+ // Example: document.getElementById('resultImage').src = url;
232
  })
233
  .catch(error => {
234
  console.error('Error:', error.message);
235
+ });`;
 
236
  }
237
 
238
  // フォーム要素の変更を監視