2017-02-06 5 views
1

で実行されているPythonスクリプトは、私は2つの引数を取りPythonのスクリプトmain.py(2テキストファイル)インポート:フラスコ

私は2.7

これをMAC OS X のPythonを使用していますがあります次のように

python main.py train.txt sample.txt 

が、私は今、非常にマイナーなHTMLでフラスコを用いた小型のフロントエンドを開発しました::で、端末上で簡単に実行さ

#front.py FLASK 
    from flask import Flask, render_template, request, redirect 
    app = Flask(__name__) 

    @app.route('/') 
    def hello_world(): 
    return render_template('index.html') 

    @app.route('/signup', methods = ['POST']) 
    def signup(): 
    email = request.form['email'] 
    email1 = request.form['email1'] 
    # command below is just for testing, I wish to implement the same as this would if this would be typed in terminal. 
    print("main.py " + email + " " + email1) 
    return redirect('/') 

if __name__ == "__main__": 
    app.run() 

とHTML

<!DOCTYPE html> 
    <html> 
    <head> 
     <title>T</title> 
    </head> 

    <body> 
     <form action="/signup" method="post"> 
     <input type="text" name="email"></input> 
     <input type="text" name="email1"></input> 
     <input type="submit" value="Signup"></input> 
     </form> 
    </body> 
    </html> 

このHTMLコードは、単純に2つの引数(私はその経験がないと私はJSよりも、この簡単に見つける)で取るためにフォームを使用しています。

私はちょうどコマンドテストに上記

print("main.py " + email + " " + email1) 

を書かれている、それが今のいずれかのユーティリティではありません。パラメータの

使用法:

#main.py 

    from filter import Filter 
    import sys 

    # Get arguments from user 
    train = sys.argv[1] 
    messages = sys.argv[2] 

    # Open files for reading and writing 
    train_file = open(train, "rb") 
    messages_file = open(messages, "rb") 
    predictions_file = open("predictions.txt", "w") 

# Create new filter and train it using the train-file 
f = Filter() 
f.train(train_file) 

#filter the messages in messages_file, write results to predictions_file 
f.filter(messages_file, predictions_file) 

# Close all the files 
train_file.close() 
messages_file.close() 
predictions_file.close() 

私は今、このフラスコアプリケーション自体を経由してmain.pyである私のスクリプトを実行したい、これが可能である方法を知りたいです。

私は別のアプリケーションデコレータsay/execを使ってimport mainを使い、127.0.0.2000から127.0.0.2000 /execになるように手動でURLを変更しましたが、これは主に引数が渡される必要があるためエラーです。

ご迷惑をおかけして申し訳ございませんが、問題を理解するために何か説明できるかどうかを教えてください。

ありがとうございました

+0

なぜmain.pyのコードを表示しないのですか? –

+0

私は、ポストの長さにちょうど追加すると感じます、それはかなり大きく、本質的にはスパム予測のベイズ分類子です。 –

+0

しかし、なぜそれをインポートして、それが定義する関数を呼び出すことはできませんか? –

答えて

2

このスクリプトは少し修正してください。あなたはそのブロックから呼び出す関数内のすべてのあなたがフラスコアプリで行うようname == '__main__'ブロック内の入力を扱うコード、残りを置く必要があります。

def do_stuff(train, messages): 
    # Open files for reading and writing 
    train_file = open(train, "rb") 
    ... 
    predictions_file.close() 

if __name__ == '__main__': 
    # Get arguments from user 
    train = sys.argv[1] 
    messages = sys.argv[2] 
    do_stuff(train, messages) 

今すぐあなたのフラスコのアプリがmain.do_stuff(email, email1)を呼び出すことができます。

+0

これは完璧に動作します!D 私が書いている最終的なファイルであるサイドノートは、そのファイルの出力をフラスコのアプリケーションに組み込むことはできますか?スクリプトが実行されてファイルが書き込まれた後、127.0.0.2000に結果ファイルが表示され、ボタンをクリックすると開始ページに戻ることができますか? –

関連する問題