🙄

SAP(ECC)の品目マスタからデータを取得してAutoCompleteしてみる

2025/03/16に公開
  1. saprfc.pyをコピー
  2. 本体(app.py)を作成する。
app.py
from flask import Flask, jsonify, request, render_template
from saprfc import main


app = Flask(__name__)
app.secret_key = "secret key"
app.config['JSON_AS_ASCII'] = False
app.config["JSON_SORT_KEYS"] = False

s = main()
@app.route('/')
def upload_form():
	return render_template('index.html')

@app.route('/search', methods=['POST'])
def search():
	MATNR = request.form['q']
	# 在庫テーブル[MARD]を抽出
	fields = ['MATNR','UMLME']
	table = 'MARD'
	# you need to put a where condition in there... could be anything
	# max number of rows to return
	maxrows = 10
	# starting row to return
	fromrow = 0
	MATNR = request.form['q']
	where = ["MATNR like '"+MATNR+"%'"]
	# query SAP
	results, headers = s.qry(fields, table, where, maxrows, fromrow)
	#必要なデータのみ取り出す
	results = [result[0].strip() for result in results]
	#json形式に変換
	resp = jsonify(results)
	#index.htmlに値を返す
	return resp
if __name__ == "__main__":
    app.run(debug = True)
  1. HTML画面の作成
index.html
<!doctype html>
<html>
<head>
	<title>Autocomplete input suggestion using Python and Flask</title>

	<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
	<!--<script src="https://code.jquery.com/jquery-3.5.1.min.js" crossorigin="anonymous"></script>-->
	<script src="https://code.jquery.com/jquery-3.6.0.min.js" crossorigin="anonymous"></script>
	<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.min.js" crossorigin="anonymous"></script>
	<script>
		$(function() {
			$("#searchBox").autocomplete({
				source : function(request, response) {
					$.ajax({
						type: "POST",
						url : "/search",
						dataType : "json",
						cache: false,
						data : {
							q : request.term
						},
						success : function(data) {
							//alert(data);
							//console.log(data);
							response(data);
						},
						error: function(jqXHR, textStatus, errorThrown) {
							console.log(textStatus + " " + errorThrown);
						}
					});
				},
				minLength : 2
			});
		});
	</script>
</head>
<body>
	<div style="width: 600px; margin: auto;">
		<h2>Autocomplete input suggestion using Python and Flask</h2>
		<p style="width: 560px; margin: auto;">
			<label>Search Here SAP ItemCode</label>&nbsp;&nbsp;<input type="text" name="search" id="searchBox"/>
		</p>
	</div>
</body>

Discussion