Running python script from django view with parameters - python

I want to call a python script with paramters from django view. The python script is stored in a subfolder (see attached picture).
Basically I have an input form and want to call crawl.py with the input data from that form. The form data should be stored in variable "production_number" in crawl.py.
view.py
from .forms import CustomerForm, LoginForm
from .models import Orders
from .ERPProgramm.crawl import crawlmain
def newOrders(request):
if request.method == 'POST':
form = CustomerForm(request.POST)
if form.is_valid():
formdata = form.cleaned_data['product_ID']
# call crawl.py with paramter formdata
return HttpResponseRedirect('/customer/newOrders')
crawl.py
import db
import sys
import requests
import json
from datetime import datetime
def query(resource):
r = requests.get('http://11.111.11.11:8080/webapp/api/v1/' + resource,
headers={'AuthenticationToken': '11111-11111-1111-1111-11111'}
)
return r
costumer_id = 1
production_number = formdata
d = query('productionOrder/?productionOrderNumber-eq={}'.format(production_number)).json()
session = db.Session()
costumer = session.query(db.Costumer).get(costumer_id)
if 'result' in d and len(d['result']) > 0:
r = d['result'][0]
order = db.Order()
try:
order.article_id = r['articleId']
order.amount = r['targetQuantity']
order.create_date = datetime.fromtimestamp(r['createdDate'] / 1000)
order.start_date = datetime.fromtimestamp(r['targetStartDate'] / 1000)
order.end_date = datetime.fromtimestamp(r['targetEndDate'] / 1000)
except NameError as e:
sys.exit('Error {}'.format(e.what()))
article_number = r['articleNumber']
d = query('article/?articleNumber-eq={}'.format(article_number)).json()
if 'result' in d and len(d['result']) > 0:
r = d['result'][0]
article_image_id = r['articleImages'][0]['id']
order.price_offer = r['articlePrices'][0]['price']
r = query('article/id/{}/downloadArticleImage?articleImageId={}'.format(order.article_id, article_image_id))
order.article_image = r.content
else:
print('No result for article with number', article_number)
costumer.orders.append(order)
session.add(costumer)
session.commit()
else:
print('No result for production order with article number', article_number)
How can I call crawl.py from django view?
Directory Overview

you can check this
In your case you need to find exact file path for doing this you need to find the path from base dir in settings.py import it in view.py .
settings.py
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
FILE_DIR = os.path.abspath(os.path.join(BASE_DIR,'/Customer/ERPProgram')
views.py
import sys
sys.path.insert(0, os.path.join(settings.FILE_DIR))
import crawl

Related

making a function that run after certain condition in python flask

I want to build a Line chatbot that when a POST requests is received and will parse the receive POST requests information to the chatbot users.
import os
import sys
from argparse import ArgumentParser
import json
from flask import Flask, request, abort,Response,jsonify
import requests
from linebot import (
LineBotApi, WebhookHandler
)
from linebot.exceptions import (
InvalidSignatureError, LineBotApiError
)
from linebot.models import (
MessageEvent, TextMessage, TextSendMessage,SourceUser
)
app = Flask(__name__)
data=''
#app.route('/data', methods=['POST'])
def get_data():
"""devicename,exceptionType,temperature,mask,stranger"""
device_name = request.form.get('device_name')
exceptionType = request.form.get('exceptionType')
temperature = request.form.get('temperature')
mask = request.form.get('mask')
stranger = request.form.get('stranger')
global data
data=f"device_name = {device_name} exceptionType = {exceptionType} temperature = {temperature} mask = {mask} stranger = {stranger}"
return_status = {"success":"true"}
return jsonify(return_status)
while len(data) > 1:
try:
line_bot_api.broadcast(
messages=TextSendMessage(text=str(data))
)
data=''
except LineBotApiError as e:
raise e
if __name__ == "__main__":
arg_parser = ArgumentParser(
usage='Usage: python ' + __file__ + ' [--port <port>] [--help]'
)
arg_parser.add_argument('-p', '--port', default=8000, help='port')
arg_parser.add_argument('-d', '--debug', default=False, help='debug')
options = arg_parser.parse_args()
app.run(debug=options.debug, port=options.port)
here I have two parts, first is the POST requests receiver. and second is the part that checks the condition if data is present. But when I send the POST requests, the line_bot_api.broadcast does not send the message to the chatbot user. I only saw examples of flask code that has a #app.route() so I was wondering if something like this works? or is there a better way to implement the condition (instead of checking data value, is it better to check if function get_data() ran or requests has been sent to our /data route)
The while loop stops immediately because the condition fails. One way to get around this and do what you want is by using threads.
import os
import sys
from argparse import ArgumentParser
import json
import threading
from flask import Flask, request, abort,Response,jsonify
import requests
from linebot import (
LineBotApi, WebhookHandler
)
from linebot.exceptions import (
InvalidSignatureError, LineBotApiError
)
from linebot.models import (
MessageEvent, TextMessage, TextSendMessage,SourceUser
)
app = Flask(__name__)
data=''
#app.route('/data', methods=['POST'])
def get_data():
"""devicename,exceptionType,temperature,mask,stranger"""
device_name = request.form.get('device_name')
exceptionType = request.form.get('exceptionType')
temperature = request.form.get('temperature')
mask = request.form.get('mask')
stranger = request.form.get('stranger')
global data
data=f"device_name = {device_name} exceptionType = {exceptionType} temperature = {temperature} mask = {mask} stranger = {stranger}"
return_status = {"success":"true"}
return jsonify(return_status)
def broadcast():
while True:
if len(data) < 1 : continue
try:
line_bot_api.broadcast(
messages=TextSendMessage(text=str(data))
)
data=''
except LineBotApiError as e:
raise e
if __name__ == "__main__":
arg_parser = ArgumentParser(
usage='Usage: python ' + __file__ + ' [--port <port>] [--help]'
)
arg_parser.add_argument('-p', '--port', default=8000, help='port')
arg_parser.add_argument('-d', '--debug', default=False, help='debug')
options = arg_parser.parse_args()
broadcast_thread = threading.Thread(target=broadcast)
broadcast_thread.start()
app.run(debug=options.debug, port=options.port)
This is definitely not the best way. But it works.

python flask app in docker is not refreshing images in frontend

I am running a python flask app on Docker. The app consists of a frontend in which a user provides a search term and the app returns an HTML file with insights and images. The problem is that after the first time I try the app, the HTML file keeps returning the images of the first try.
If I erase cookies, the problem is solved. But I cannot ask a user to erase cookies every time she needs to use the app.
I tried using os.remove before saving every image, but this doesn't work.
from googleapiclient.discovery import build
import pandas as pd
import matplotlib.pyplot as plt
from flask_restful import Api
from flask import Flask,render_template,request
import math
application = Flask(__name__)
api = Api(application)
#application.route('/')
def home():
return render_template('home.html')
#application .route('/predict',methods=['POST'])
def predict():
term1 = request.form['term1']
my_results_list = []
def google_query(query, api_key, cse_id, **kwargs):
query_service = build("customsearch", "v1", developerKey=api_key)
query_results = query_service.cse().list(q=query, cx=cse_id, **kwargs).execute()
return query_results['items']
my_results = google_query(term1,'api_key','cse_id', num = 10)
for result in my_results:
my_results_list.append(result['link'])
data = pd.DataFrame(my_results_list)
values = [1] * len(data)
N = len(values)
values += values[:1]
angles = [n/float(N)*2*math.pi for n in range(N)]
angles += angles[:1]
plt.polar(angles,values, marker='.', color = 'red')
temp = 'graph'
temp = 'static/' + temp + '.png'
plt.savefig(temp)
html = []
html.append("""<!DOCTYPE html><html>""")
html.append("""<img src="{{url_for('static',filename='graph.png')}}"/>""")
html = ' '.join(html)
temp = 'templates/result_vf.html'
print(html, file = open(temp,'w',encoding='utf-8'))
return render_template('result_vf.html')
if __name__ == '__main__':
application.run(host=('0.0.0.0'), port=8080 ,debug=True)

DJANGO : Can't list the content of a folder located in static directory in my django project

In my project I have a folder datasets in the static folder that content 2 others folders. I want to list the content of each of thosefolders and return it in HttpResponse. I define a utility function list_dir_content in utils/data.py where I use glob.glob() function by passing it paths of those folders but I recieve an empty result: an empty list [] from the glob.glob() function. How can I fix thet issue regardless of the os (I'm developping my internship project either on ubuntu or windows 10). Thanks!
Here are the structure of my project and files views.py, models.py
You can also see the code which call the utility function list_dir_content
#in views.py
def server_uts_datasets(request):
if request.method == 'GET':
uts_datasets = Dataset.get_uts_datasets()
uts_datasets_serializer = DatasetSerializer(uts_datasets, many=True)
print(uts_datasets)
return JsonResponse(uts_datasets_serializer.data, safe=False)
#in models.py
#classmethod
def get_mts_datasets(cls):
mts_datasets_files = data.list_dir_content(settings.DATASETS_DIR)
mts_datasets = []
for mts_datasets_file in mts_datasets_files:
dataset_type = 'mts'
dataset_path = mts_datasets_file
dataset_name = data.get_dataset_name(mts_datasets_file)
dataset_nb_instances = data.get_nb_instances(mts_datasets_file)
mts_dataset = Dataset(dataset_path = dataset_path, dataset_name = dataset_name, dataset_nb_instances = dataset_nb_instances, dataset_type = dataset_type)
mts_datasets.append(mts_dataset)
return mts_datasets
#in data.py
import glob
import os
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
def list_dir_content(dir_path, file_extension=''):
files_pattern = dir_path + '/*.' + file_extension
print("files_pattern : ", files_pattern)
#files_results_paths = glob.glob(files_pattern)
files_results_paths = glob.glob('../tsanalysisapp/static/tsanalysisapp/datasets/uts/*.')
print("files_results_paths : ", files_results_paths)
return files_results_paths
#in settings.py
MTS_DATASETS_DIR = os.path.join(BASE_DIR, 'tsanalysisapp/static/tsanalysisapp/datasets/mts') # paths to datasets in tne server
UTS_DATASETS_DIR = os.path.join(BASE_DIR, 'tsanalysisapp/static/tsanalysisapp/datasets/uts') # paths to datasets in tne server
Thanks in advance for your kindly help...
structure of my django project
Just only replace in the os.path.join() function all the slashes / with ', ' that result in os.path.join('parent_dir_name', 'subdir_name', ..., 'last_dir_name', ''):
MTS_DATASETS_DIR = os.path.join(BASE_DIR, 'tsanalysisapp', 'static', 'tsanalysisapp', 'datasets', 'mts', '*.')

Python-Flask - render template within a class gives 404

I am trying to run python with html using flask locally, i try to render a template within a class object, it compiles but when i try to access it on http://localhost:5000 it gives me 404. Could anyone tell what I'm doing wrong here?
I am trying to display values from a json format using chart.js library.
from flask import Flask
from flask import render_template
import os.path
import json
app = Flask(__name__)
#import the json file
_player_json_file = os.path.join(os.path.dirname(__file__), 'players.json')
def read_players(player_file=None):
if player_file is None:
player_file = _player_json_file
try:
data = json.loads(open(player_file).read())
except IOError:
return {}
#make player dictionary
players = {}
#get player ID from json file = data.
for playerID in data:
players[playerID] = Player_data(data[playerID])
return players
class Players_Data (object):
#app.route("/")
def __init__(self, data):
"""
Assign all the values from the json file to new variables
the values are birth date age weight...
"""
self.player_ID = data['gsis_id']
self.gsis_name = data.get('gsis_name', '')
self.player_fullname = data.get('full_name', '')
self.player_first_name = data.get('first_name', '')
self.player_last_name = data.get('last_name', '')
self.player_weight = data.get('weight','')
self.player_height = data.get('height' , '')
self.player_birth = data.get('birthdate', '')
self.player_pro_years = data.get('years_pro', '')
self.player_team = data.get('data', '')
values = [player_ID,gsis_name,player_fullname,player_first_name,player_last_name,player_weight]
return render_template('chart.html', data=data, values=values)
if __name__ == "__main__":
app.run(host='localhost')
I don't think template rendering is the issue here. You can just return a string from your view as an example.
You get a 404 because that's not how you do classy views in flask. You can have a look here: http://flask.pocoo.org/docs/0.12/views/
But essentially you have to extend from flask.views.View and then manually attach your route to app instance, so it looks something like this:
from flask import Flask
from flask.views import View
app = Flask(__name__)
class Players_Data(View):
def dispatch_request(self):
return 'LOL'
app.add_url_rule('/', view_func=Players_Data.as_view('show_users'))
if __name__ == "__main__":
app.run(host='localhost')

Pass variables between different scripts python

I made a script python app.py and I managed to store data from a javascript form, thanks to a flask app :
app = Flask(__name__)
app.config.from_object('config')
db.init_app(app)
#app.route('/getFormData', methods=['POST'])
def get_javascript_data():
params = request.form.to_dict()
sunElevation = params['sunElevation']
cloudCoverage = params['cloudCoverage']
thresholdNDVI = params['thresholdNDVI']
limitScene = params['limitScene']
city = params['city']
data_search = passData(sunElevation, cloudCoverage, thresholdNDVI, limitScene, city)
return jsonify(data_search.data_dict)
if __name__ == '__main__':
app.run()
Here is the definition of the class passData, in models.py :
class passData:
def __init__(self, sunElevation, cloudCoverage, thresholdNDVI, limitScene, city):
self.sunElevation = sunElevation
self.cloudCoverage = cloudCoverage
self.thresholdNDVI = thresholdNDVI
self.limitScene = limitScene
self.city = city
self.data_dict = [{'sunElevation':self.sunElevation,'cloudCoverage':self.cloudCoverage, 'thresholdNDVI':self.thresholdNDVI, 'limit':self.limitScene, 'city':self.city}]
I need to use those different parameters (sunElevation...) in an other script, in a other folder, to execute a search of imagery and then run the script associated. My problem is that I don't know how to pass those data, because they seem to only exist in the fonction defined in the get_javascript_data().
If someone has an idea that could help me !
You just have to import the other script, call the function and pass the parameters:
app = Flask(__name__)
app.config.from_object('config')
db.init_app(app)
from path.to.your.file import your_function
#app.route('/getFormData', methods=['POST'])
def get_javascript_data():
params = request.form.to_dict()
sunElevation = params['sunElevation']
cloudCoverage = params['cloudCoverage']
thresholdNDVI = params['thresholdNDVI']
limitScene = params['limitScene']
city = params['city']
data_search = passData(sunElevation, cloudCoverage, thresholdNDVI, limitScene, city)
# call the function and pass the parameters
your_function(sunElevation, cloudCoverage) #...
return jsonify(data_search.data_dict)
Try adding the following code in your file, then import the script
import os, sys
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '..', '..')))
from eosLandviewer.main_naturalite import search

Categories

Resources