D2MatE Top  this page

tkCGIApplicaitonの使い方

CGIスクリプト: atom_info.py

Templateの例

<html lang="ja">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <title>原子情報取得アプリ</title>
</head>
        
<body>
<script>
  const script_url = "{{ script_url }}";
  const next_action = "{{ next_action }}";

  async function fetchAtomInfo(event) {
    event.preventDefault(); // フォームのデフォルト送信を防ぐ

    const formData = new FormData(event.target);
    const params = new URLSearchParams(formData);
    params.append("action", `${next_action}`);

    try {
      const response = await fetch(script_url, {
      method: 'POST',
      body: params
      });

      if (response.ok) {
        const jsonData = await response.json();
        let formattedData = "";
        for (const [key, value] of Object.entries(jsonData)) {
          formattedData += `${key}: ${value}\n`;
        }
        document.getElementById('response').value = formattedData;
      } else {
        document.getElementById('response').value = "エラー: サーバーからの応答が正しくありません";
      }
    } catch (error) {
      document.getElementById('response').value = `エラー: ${error.message}`;
    }
  }
</script>

<h1>原子情報取得アプリ atom_info.py</h1>
<form onsubmit="fetchAtomInfo(event)">
  <label for="atom_name">atom name:</label>
  <input type="text" id="atom_name" name="atom_name" 
           placeholder="input atom name/number" 
           value="Na" required size="20">
  <br><br>
  <button type="submit">Show atom info</button>
</form>

<br>
<label for="response">Response:</label><br>
<textarea id="response" rows="20" cols="80" readonly></textarea>

</body>
</html>