使用者:游士賢:修訂版本之間的差異

出自福留子孫
跳轉到: 導覽搜尋
 
(未顯示同用戶所作出之10次版本)
第 1 行: 第 1 行:
===Stable Diffusion單機版安裝===
+
==php+python顯示於網頁==
*下載並安裝 [https://www.python.org/downloads/release/python-3106/ python3.10.6]
+
===範例一、文字顯示===
*下載並安裝 [https://git-scm.com/downloads git]。
+
*說明:利用 php 讀取 python 檔案,並將文字顯示「Hello, world!」於頁面上。
*下載並安裝 stable-diffusion-webui,開啟命令字元,輸入「git clone https://github.com/AUTOMATIC1111/stable-diffusion-webui.git」。
+
*範例程式:[http://jendo.org/~游士賢/chatgcp/02 連結]
*請進入「stable-diffusion-webui」的資料夾,找到「webui-user」請貼上以下程式碼,修改並儲存。
+
 
 +
'''index.php,程式碼如下:'''
 
<pre>
 
<pre>
@echo off
 
  
set PYTHON=C:\User\...\python.exe (請填入 python3.10 執行檔的路徑)
+
<!DOCTYPE html>
set GIT=
+
<html>
set VENV DIR=
+
<head>
Set COMMANDI INE_ARGS=--no-half --skip-torch-cuda-test (如有獨立顯卡,無需填寫)
+
    <meta charset="UTF-8">
git pull
+
    <title>PHP呼叫Python範例</title>
call webui.bat
+
</head>
 +
<body>
 +
<?php
 +
    $output = shell_exec('python word.py');    // 用 shell_exec 函式呼叫 Python 程式並取得輸出
 +
    echo "<pre>$output</ pre>";                // 將輸出作為純文本輸出到 HTML 頁面上
 +
?>
 +
</body>
 +
</html>
 +
 
 
</pre>
 
</pre>
*請進入「stable-diffusion-webui」的資料夾,點選「webui-user」批次檔案,下載並安裝「webui-user」。
 
*安裝完成,會給一組伺服器IP(127.0.0.1:7861),請複製並用瀏覽器開啟。
 
  
===Stable Diffusion 介面介紹===
+
'''word.py,程式碼如下:'''
介面輸入只能用英文<br/><img src='http://jendo.org/~游士賢/使用說明/StableDiffusion/StableDiffusion_使用介面.png' width='1000px' />
+
<pre>
 +
print("Hello, world!")
 +
</pre>
 +
 
 +
===範例二、兩數相加算術===
 +
*說明:利用 php 讀取 python 檔案,並使用者將兩個數值填入並按計算,答案將顯示於網頁上。
 +
*範例程式:[http://jendo.org/~游士賢/chatgcp/03 連結]
 +
 
 +
'''index.php,程式碼如下:'''
 +
<pre>
 +
<!DOCTYPE html>
 +
<html>
 +
<head>
 +
<title>兩數相加算術範例</title>
 +
</head>
 +
<body>
 +
<h2>兩數相加算術</h2>
 +
<form method="POST">
 +
<label>請輸入第一個數字:</label>
 +
<input type="text" name="num1">
 +
<br><br>
 +
<label>請輸入第二個數字:</label>
 +
<input type="text" name="num2">
 +
<br><br>
 +
<input type="submit" name="submit" value="計算">
 +
</form>
 +
<?php
 +
if(isset($_POST['submit'])){
 +
$num1 = $_POST['num1'];
 +
$num2 = $_POST['num2'];
 +
 
 +
$python_path = "/usr/bin/python"; // 設定 Python 可執行文件的路徑
 +
$python_script = "calculate.py"; // 要執行的 Python 腳本路徑
 +
 
 +
// 執行 Python 腳本,並傳遞數字參數
 +
$command = escapeshellcmd("$python_path $python_script $num1 $num2");
 +
$output = shell_exec($command);
 +
 
 +
echo "<p>答案:$output</p>"; // 在網頁上顯示 Python 腳本的輸出
 +
}
 +
?>
 +
</body>
 +
</html>
 +
</pre>
 +
 
 +
'''calculate.py,程式碼如下:'''
 +
<pre>
 +
import sys
 +
 
 +
# 獲取從 PHP 傳遞的兩個數字參數
 +
num1 = float(sys.argv[1])
 +
num2 = float(sys.argv[2])
 +
 
 +
# 執行算術操作
 +
result = num1 + num2
 +
 
 +
# 將結果返回給 PHP
 +
print(result)
 +
</pre>
 +
 
 +
==php+xml顯示於網頁==
 +
===範例一、已知兩數,應用php讀取xml數值===
 +
*說明:xml 檔案已存在兩位數「12」和「34」,應用php讀取xml檔,將答案將顯示於網頁上。
 +
*範例程式:[http://jendo.org/~游士賢/chatgcp/06 連結]
 +
 
 +
'''index.php,程式碼如下:'''
 +
<pre>
 +
<?php
 +
    // 讀取 XML 檔案
 +
    $xml = simplexml_load_file("numbers.xml");
 +
   
 +
    // 取出兩個數字
 +
    $num1 = (int)$xml->number1;
 +
    $num2 = (int)$xml->number2;
 +
   
 +
    // 計算結果
 +
    $result = $num1 + $num2;
 +
   
 +
    // 回傳結果
 +
    echo "$num1 + $num2 = $result";
 +
?>
 +
</pre>
 +
 
 +
'''numbers.xml,程式碼如下:'''
 +
<pre>
 +
<?xml version="1.0" encoding="UTF-8"?>
 +
<numbers>
 +
    <number1>12</number1>
 +
    <number2>34</number2>
 +
</numbers>
 +
</pre>
 +
 
 +
===範例二、未知兩數,應用php讀取xml數值===
 +
*說明:使用者自行輸入兩個數字進行加法運算,按下計算按鈕後,系統會顯示計算結果,同時將計算紀錄存入 XML 檔案中。
 +
*範例程式:[http://jendo.org/~游士賢/chatgcp/05 連結]
 +
 
 +
'''index.php,程式碼如下:'''
 +
<pre>
 +
<!DOCTYPE html>
 +
<html>
 +
<head>
 +
    <meta charset="UTF-8">
 +
    <title>PHP 結合 XML 的算術範例程式</title>
 +
</head>
 +
<body>
 +
    <h1>請輸入兩個數字進行運算</h1>
 +
    <form action="calculate.php" method="post">
 +
        <label for="num1">第一個數字:</label>
 +
        <input type="number" name="num1" id="num1"><br><br>
 +
        <label for="num2">第二個數字:</label>
 +
        <input type="number" name="num2" id="num2"><br><br>
 +
        <label for="operator">運算符號:</label>
 +
        <select name="operator" id="operator">
 +
            <option value="+">+</option>
 +
            <option value="-">-</option>
 +
        </select><br><br>
 +
        <input type="submit" value="計算">
 +
    </form>
 +
</body>
 +
</html>
 +
</pre>
 +
 
 +
'''calculate.php,程式碼如下:'''
 +
<pre>
 +
<?php
 +
// 讀取 XML 檔案
 +
$xml = simplexml_load_file('result.xml');
 +
 
 +
// 取得使用者輸入的數字和運算符號
 +
$num1 = $_POST['num1'];
 +
$num2 = $_POST['num2'];
 +
$operator = $_POST['operator'];
 +
 
 +
// 計算結果
 +
if ($operator == '+') {
 +
    $result = $num1 + $num2;
 +
} else {
 +
    $result = $num1 - $num2;
 +
}
 +
 
 +
// 建立新的計算紀錄
 +
$calculation = $xml->addChild('calculation');
 +
$calculation->addChild('num1', $num1);
 +
$calculation->addChild('num2', $num2);
 +
$calculation->addChild('operator', $operator);
 +
$calculation->addChild('result', $result);
 +
 
 +
// 將結果存入 XML 檔案
 +
$xml->asXML('result.xml');
 +
 
 +
// 顯示計算結果
 +
echo "計算結果:$num1 $operator $num2 = $result";
 +
?>
 +
</pre>
 +
 
 +
'''result.xml,程式碼如下:'''
 +
<pre>
 +
<?xml version="1.0" encoding="UTF-8"?>
 +
<calculations>
 +
</calculations>
 +
</pre>
 +
 
 +
==php+json顯示於網頁==
 +
===範例一、使用者自行輸入兩個數字進行,加減法功能===
 +
*說明:使用者輸入的兩個數字和運算符號,然後進行加法或減法運算。計算結果存儲在一個 PHP 陣列中,並使用 PHP 內置的 json_encode 函數將其轉換為 JSON 格式的字串。最後,透過設定 Content-Type 為 application/json,回傳 JSON 格式的計算結果。
 +
*範例程式:[http://jendo.org/~游士賢/chatgcp/07 連結]
 +
 
 +
'''index.html,程式碼如下:'''
 +
<pre>
 +
<!DOCTYPE html>
 +
<html>
 +
<head>
 +
<meta charset="utf-8" />
 +
<title>加減法運算</title>
 +
</head>
 +
<body>
 +
<h1>加減法運算</h1>
 +
<form action="calculate.php" method="post">
 +
<p>
 +
<label for="num1">輸入第一個數字:</label>
 +
<input type="number" id="num1" name="num1" required>
 +
</p>
 +
<p>
 +
<label for="num2">輸入第二個數字:</label>
 +
<input type="number" id="num2" name="num2" required>
 +
</p>
 +
<p>
 +
<label for="operator">選擇運算符號:</label>
 +
<select id="operator" name="operator">
 +
<option value="+">+</option>
 +
<option value="-">-</option>
 +
</select>
 +
</p>
 +
<p>
 +
<input type="submit" value="計算">
 +
</p>
 +
</form>
 +
</body>
 +
</html>
 +
</pre>
 +
 
 +
'''calculate.php,程式碼如下:'''
 +
<pre>
 +
<?php
 +
// 取得使用者輸入的數字和運算符號
 +
$num1 = $_POST['num1'];
 +
$num2 = $_POST['num2'];
 +
$operator = $_POST['operator'];
 +
 
 +
// 計算結果
 +
if ($operator == '+') {
 +
    $result = $num1 + $num2;
 +
} else {
 +
    $result = $num1 - $num2;
 +
}
 +
 
 +
// 將計算結果存入 JSON 格式的字串
 +
$data = array(
 +
    'num1' => $num1,
 +
    'num2' => $num2,
 +
    'operator' => $operator,
 +
    'result' => $result
 +
);
 +
$json = json_encode($data);
 +
 
 +
// 設定 Content-Type 為 JSON
 +
header('Content-Type: application/json');
 +
 
 +
// 回傳 JSON 格式的計算結果
 +
echo $json;
 +
?>
 +
</pre>

2023年3月15日 (三) 14:56的最新修訂版本

php+python顯示於網頁

範例一、文字顯示

  • 說明:利用 php 讀取 python 檔案,並將文字顯示「Hello, world!」於頁面上。
  • 範例程式:連結

index.php,程式碼如下:


<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>PHP呼叫Python範例</title>
</head>
<body>
<?php
    $output = shell_exec('python word.py');    // 用 shell_exec 函式呼叫 Python 程式並取得輸出
    echo "<pre>$output</ pre>";                // 將輸出作為純文本輸出到 HTML 頁面上
?>
</body>
</html>

word.py,程式碼如下:

print("Hello, world!")

範例二、兩數相加算術

  • 說明:利用 php 讀取 python 檔案,並使用者將兩個數值填入並按計算,答案將顯示於網頁上。
  • 範例程式:連結

index.php,程式碼如下:

<!DOCTYPE html>
<html>
<head>
	<title>兩數相加算術範例</title>
</head>
<body>
	<h2>兩數相加算術</h2>
	<form method="POST">
		<label>請輸入第一個數字:</label>
		<input type="text" name="num1">
		<br><br>
		<label>請輸入第二個數字:</label>
		<input type="text" name="num2">
		<br><br>
		<input type="submit" name="submit" value="計算">
	</form>
	<?php
		if(isset($_POST['submit'])){
			$num1 = $_POST['num1'];
			$num2 = $_POST['num2'];

			$python_path = "/usr/bin/python";	// 設定 Python 可執行文件的路徑
			$python_script = "calculate.py";	// 要執行的 Python 腳本路徑

			// 執行 Python 腳本,並傳遞數字參數
			$command = escapeshellcmd("$python_path $python_script $num1 $num2");
			$output = shell_exec($command);

			echo "<p>答案:$output</p>";		// 在網頁上顯示 Python 腳本的輸出
		}
	?>
</body>
</html>

calculate.py,程式碼如下:

import sys

# 獲取從 PHP 傳遞的兩個數字參數
num1 = float(sys.argv[1])
num2 = float(sys.argv[2])

# 執行算術操作
result = num1 + num2

# 將結果返回給 PHP
print(result)

php+xml顯示於網頁

範例一、已知兩數,應用php讀取xml數值

  • 說明:xml 檔案已存在兩位數「12」和「34」,應用php讀取xml檔,將答案將顯示於網頁上。
  • 範例程式:連結

index.php,程式碼如下:

<?php
    // 讀取 XML 檔案
    $xml = simplexml_load_file("numbers.xml");
    
    // 取出兩個數字
    $num1 = (int)$xml->number1;
    $num2 = (int)$xml->number2;
    
    // 計算結果
    $result = $num1 + $num2;
    
    // 回傳結果
    echo "$num1 + $num2 = $result";
?>

numbers.xml,程式碼如下:

<?xml version="1.0" encoding="UTF-8"?>
<numbers>
    <number1>12</number1>
    <number2>34</number2>
</numbers>

範例二、未知兩數,應用php讀取xml數值

  • 說明:使用者自行輸入兩個數字進行加法運算,按下計算按鈕後,系統會顯示計算結果,同時將計算紀錄存入 XML 檔案中。
  • 範例程式:連結

index.php,程式碼如下:

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>PHP 結合 XML 的算術範例程式</title>
</head>
<body>
    <h1>請輸入兩個數字進行運算</h1>
    <form action="calculate.php" method="post">
        <label for="num1">第一個數字:</label>
        <input type="number" name="num1" id="num1"><br><br>
        <label for="num2">第二個數字:</label>
        <input type="number" name="num2" id="num2"><br><br>
        <label for="operator">運算符號:</label>
        <select name="operator" id="operator">
            <option value="+">+</option>
            <option value="-">-</option>
        </select><br><br>
        <input type="submit" value="計算">
    </form>
</body>
</html>

calculate.php,程式碼如下:

<?php
// 讀取 XML 檔案
$xml = simplexml_load_file('result.xml');

// 取得使用者輸入的數字和運算符號
$num1 = $_POST['num1'];
$num2 = $_POST['num2'];
$operator = $_POST['operator'];

// 計算結果
if ($operator == '+') {
    $result = $num1 + $num2;
} else {
    $result = $num1 - $num2;
}

// 建立新的計算紀錄
$calculation = $xml->addChild('calculation');
$calculation->addChild('num1', $num1);
$calculation->addChild('num2', $num2);
$calculation->addChild('operator', $operator);
$calculation->addChild('result', $result);

// 將結果存入 XML 檔案
$xml->asXML('result.xml');

// 顯示計算結果
echo "計算結果:$num1 $operator $num2 = $result";
?>

result.xml,程式碼如下:

<?xml version="1.0" encoding="UTF-8"?>
<calculations>
</calculations>

php+json顯示於網頁

範例一、使用者自行輸入兩個數字進行,加減法功能

  • 說明:使用者輸入的兩個數字和運算符號,然後進行加法或減法運算。計算結果存儲在一個 PHP 陣列中,並使用 PHP 內置的 json_encode 函數將其轉換為 JSON 格式的字串。最後,透過設定 Content-Type 為 application/json,回傳 JSON 格式的計算結果。
  • 範例程式:連結

index.html,程式碼如下:

<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8" />
	<title>加減法運算</title>
</head>
<body>
	<h1>加減法運算</h1>
	<form action="calculate.php" method="post">
		<p>
			<label for="num1">輸入第一個數字:</label>
			<input type="number" id="num1" name="num1" required>
		</p>
		<p>
			<label for="num2">輸入第二個數字:</label>
			<input type="number" id="num2" name="num2" required>
		</p>
		<p>
			<label for="operator">選擇運算符號:</label>
			<select id="operator" name="operator">
				<option value="+">+</option>
				<option value="-">-</option>
			</select>
		</p>
		<p>
			<input type="submit" value="計算">
		</p>
	</form>
</body>
</html>

calculate.php,程式碼如下:

<?php
// 取得使用者輸入的數字和運算符號
$num1 = $_POST['num1'];
$num2 = $_POST['num2'];
$operator = $_POST['operator'];

// 計算結果
if ($operator == '+') {
    $result = $num1 + $num2;
} else {
    $result = $num1 - $num2;
}

// 將計算結果存入 JSON 格式的字串
$data = array(
    'num1' => $num1,
    'num2' => $num2,
    'operator' => $operator,
    'result' => $result
);
$json = json_encode($data);

// 設定 Content-Type 為 JSON
header('Content-Type: application/json');

// 回傳 JSON 格式的計算結果
echo $json;
?>