At the beginning.
I am Japanese. English may be inadequate. I look forward to working with you.
What I want to achieve.
I'm currently developing a web application and I'm using Flask. And I'm developing with Rasspberry pi. I want to execute the "message.py" file inside the USB connected as an external storage from the "app.py" file saved in Raspberry PI.
File description.
①「app.py」...Executable file stored on Raspberry Pi. If you enter characters in the text box displayed here and send, the value will be sent to "judg.py".
②「judg.py」...Find and execute the file to be executed from the external storage according to the value sent.
③「message.py」...This is one of the applications stored in external storage.
Code 「app.py」
from flask import Flask, render_template, request
import judg
app = Flask(__name__)
@app.route('/')
def index():
return render_template('index.html', message="what shall I do?")
@app.route('/', methods=['POST'])
def form():
field = request.form['field']
if request.method == 'POST':
res = judg.judg(field)
return render_template('index.html', message=res['msg'], answer=res['ans'])
if __name__=='__main__':
app.run(debug=True, host='localhost')
「judg.py」
from flask import Flask, render_template, request, redirect
import os, sys
sys.path.append(os.path.join(os.path.dirname(__file__), '/media/pi/NAS'))
import message
app = Flask(__name__)
@app.route('/')
def judg(field):
if "message" in field:
res = {'msg':"What shall I do?", 'ans':"message is Active!"}
message.login()
return res
else:
res = {'msg':"What shall I do?", 'ans':"Sorry.."}
return res
「message.py」
from flask import Flask, render_template, request, session, redirect
app = Flask(__name__)
app.secret_key = b'abc'
member_data = {}
message_data = []
@app.route('/', methods=['GET'])
def index():
global message_data
if 'login' in session and session['login']:
msg = 'Login id:' + session['id']
return render_template('messages.html',
title='Messages',
message=msg,
data=message_data )
else:
return redirect('/login')
@app.route('/', methods=['POST'])
def form():
msg = request.form.get('comment')
message_data.append((session['id'], msg))
if len(message_data) > 25:
message_data.pop(0)
return redirect('/')
#login page access
@app.route('/login', methods=['GET'])
def login():
return render_template('login.html',
title='Login',
err=False,
message='IDとパスワ−ドを入力:',
id='' )
#login
@app.route('/login', methods=['POST'])
def login_post():
global member_data
id = request.form.get('id')
pswd = request.form.get('pass')
if id in member_data:
if pswd == member_data[id]:
session['login'] = True
else:
session['login'] = False
else:
member_data[id] = pswd
session['login'] = True
session['id'] = id
if session['login']:
return redirect('/')
else:
return render_template('login.html',
title='Login',
err=False,
message='パスワードが違います。',
id=id )
#logout
@app.route('/logout', methods=['GET'])
def logout():
session.pop('id', None)
session.pop('login')
return redirect('/login')
if __name__== '__main__':
app.run(debug=True, host='localhost', port=5000)
Directory structure
「WebServer」 →(app.py), (judg.py), (「templates」→login.html, layout.html, index.html), (「static」→style.css)
「Skills」(External storage) →(message.py), (「templates」→layout.html, message.html), (「static」→style.css)
What's happening now
The behavior I expect is that typing "message" in the textbox will launch "message.py" and take you to the application login page. I want to open it in another tab if possible. Currently, typing "message" does not cause an error, but instead of jumping to the login page, the message field on the original page says "message is active".
Finally
Since I am still a beginner in web applications, I would appreciate it if you could point out any mistakes. If you're missing any information, let me know and I'll add it. Thank you for browsing.