Python Code on my XAMPP Website won´t work - python

from flask import Flask, render_template, request
import random
import datetime
app = Flask(__name__)
# List to store the submitted URLs
urls = []
#app.route('/')
def index():
return render_template('index.html')
#app.route('/submit', methods=['POST'])
def submit():
url = request.form['url']
# Add the URL to the list and return a success message
urls.append(url)
return "URL submitted successfully!"
#app.route('/stream')
def stream():
# Select a random URL from the last 20 days
now = datetime.datetime.now()
twenty_days_ago = now - datetime.timedelta(days=20)
recent_urls = [url for url in urls if url.submission_time > twenty_days_ago]
current_song_url = random.choice(recent_urls)
return render_template('stream.html', url=current_song_url)
if __name__ == '__main__':
app.run(debug=True)
I want to use this Code for my XAMPP Website (Html/php mostly used) but it only shows the code. So I watched some tutorials with config stuff and all that but then there is an internal server error. What should I do?
I tried to config Apache (httpd.conf) and installed everything (Python, Flask etc.)

Related

handling Links and CSS in Flask web proxy

So I tried to develop a web proxy using Flask. in the progress I found out that somehow I need to handle Likns so all redirects take place in the web proxy, this is what I write:
import requests
from flask import Flask , request
from requests import get
import re
app = Flask(__name__)
mainURL = ""
myHost = "http://127.0.0.1:5000/"
#app.route("/", methods =["GET", "POST"])
def Home():
#a small form for users to write thier URL inside it
mainpage = '<!DOCTYPE html><html><body><form action="/" method="post"><label for="url">URL:</label><br><input type="text" id="url" name="url" value="https://www.google.com/"><br><input type="submit" value="Open"></form></body></html>'
#check if submit button trigered
if request.method == "POST":
mainURL = request.form.get("url")
response = requests.get(mainURL)
#this is for handling links and pictures. I know its not optimal, thats why im here!
a = response.text.replace("='/",f"='{mainURL}")
a = a.replace('="/',f'="{mainURL}')
a = a.replace("url(/",f"url({mainURL}")
a = a.replace('href="http',f'href="{myHost}http')
a = a.replace("href='http",f"href='{myHost}http")
a = a.replace(r'href="(?!http)',f'href="{myHost}{mainURL}')
a = a.replace(r"href='(?!http)",f"href='{myHost}{mainURL}")
return a
return mainpage
#decroator for the times when a path opened
#app.route('/<path:path>')
def proxy(path):
#this RegEx find the website FQDN from path
if re.match(r"https?://w{3}\.\w*\.\w*/",path):
temp = re.match(r"https?://w{3}\.\w*\.\w*/",path)
mainURL = temp[0]
response = requests.get(path)
#again links and pictures handler. dont judge me, I wrote a function for it but IDK why it didn't works for pictures!
a = response.text.replace("='/",f"='{mainURL}")
a = a.replace('="/',f'="{mainURL}')
a = a.replace("url(/",f"url({mainURL}")
a = a.replace('href="http',f'href="{myHost}http')
a = a.replace("href='http",f"href='{myHost}http")
a = a.replace(r'href="(?!http)',f'href="{myHost}{mainURL}')
a = a.replace(r"href='(?!http)",f"href='{myHost}{mainURL}")
return a
return "URL is Incorrect!"
if __name__ == "__main__":
app.run(host='127.0.0.1',port='5000',debug=True)
This is Output:
opening Google using proxy
so its open a webpage but its so slow because of all those replace and with all of these its still can't load CSS!
so what I want is a optimized way to redirect all links to proxy and handling CSS!

Dialogflow response from webhook using flask python integration platform digital human uneeq

I am trying to send a response to Dialogflow Es using webhook written in python-flask.
Integration platform :
digital human --> Uneeq
I tried with the fulfillment library (dialogflow_fulfillment) but it was not working so I tried it without a library like :
if req.get('queryResult').get('action') == 'appointment':
print("ïntent triggered")
return {"fulfillmentText": 'BUDDY!'}
It worked!
Now the issue is before integrating this chatbot to digital human I wrote the whole code using the fulfillment library. Now I need to change the whole code for this purpose.
As I am unable to extract entities using context-based intents (session-vars) like :
case = agent.context.get('session-vars').get('parameters').get('case')
name = agent.context.get('session-vars').get('parameters').get('name')['name']
email = agent.context.get('session-vars').get('parameters').get('email')
phone = agent.context.get('session-vars').get('parameters').get('phone')
So my question is how to accomplish this task successfully?
If I try to use the fulfillment library then how to send a response to Dialogflow so that digital human will understand it since "agent.add" doesn't work.
Or, If I try to do it without a library then how to extract entities(from the session-vars) like (case, name, email, phone).
I need to save all these parameters(entities) in firebase, These output context (only session-vars parameters not other contexts):
This is the response I am getting correctly without library:
Unable to proceed at any cost!
looking forward to your response.
Thanks in advance!
Complete code (with the library):
from dialogflow_fulfillment import WebhookClient
from flask import Flask, request, Response
import json
app = Flask(__name__)
def handler(agent: WebhookClient) :
"""Handle the webhook request.."""
req = request.get_json(force=True)
a= req.get('queryResult').get('action')
if req.get('queryResult').get('action') == 'appointment':
name = agent.context.get('session-vars').get('parameters').get('name')['name']
email = agent.context.get('session-vars').get('parameters').get('email')
phone = agent.context.get('session-vars').get('parameters').get('phone')
print("ïntent triggered")
agent.add('BUDDY!')
#app.route('/webhook', methods=['GET', 'POST'])
def webhook():
req = request.get_json(force=True)
agent = WebhookClient(req)
agent.handle_request(handler)
return agent.response
if __name__ == '__main__':
app.run(debug=True)
Complete code (without library):
import urllib
import json
import os
from flask import Flask
from flask import request
from flask import make_response
# Flask app should start in global layout
app = Flask(__name__)
#app.route('/webhook', methods=['POST'])
def webhook():
req = request.get_json(silent=True, force=True)
res = makeWebhookResult(req)
res = json.dumps(res, indent=4)
r = make_response(res)
#print(res)
r.headers['Content-Type'] = 'application/json'
return r
def makeWebhookResult(req):
intent_name = req.get('queryResult').get('intent').get('displayName')
print(intent_name)
action_name = req['queryResult']['action']
if req.get('queryResult').get('action') == 'input.welcome':
print("intent trigered")
return {"fulfillmentText": 'Hi there'}
if action_name == 'test':
cont = req.get('queryResult').get('outputContexts')[0]['name']
print(type(cont))
x = cont.split("/")
print(x[6])
if x[6] == 'session-vars' :
para = req['queryResult']['outputContexts']
print(para)
print("test intent trigered")
return {"fulfillmentText": 'Bye there'}
if __name__ == '__main__':
app.run(debug=True)

Extracting image then thumbnail that image and display on index.html

So I made my mind to use python goose for extracting the main image when url is inserted. I found good example here https://blog.openshift.com/day-16-goose-extractor-an-article-extractor-that-just-works/ the blog example is using flask, I tried to make the script for people using django
Here is my media.py ( not sure if this works yet )
media.py
import json
from goose import Goose
def extract(request):
url = request.args.get('url')
g = Goose()
article = g.extract(url=url)
resposne = {'image':article.top_image.src}
return json.dumps(resposne)
How do I use the image I get from above and put it in a thumbnail and display that on the index.html ? please, any help would be very appreciated. Merry Christmas
Edit 1 : I tried to convert below to django script but in my case only script image
from flask import Flask
from flask import jsonify
from flask import render_template
from flask import request
from goose import Goose
app = Flask(__name__)
#app.route('/')
#app.route('/index')
def index():
return render_template('index.html')
#app.route('/api/v1/extract')
def extract():
url = request.args.get('url')
g = Goose()
article = g.extract(url=url)
response = {'title' : article.title , 'text' : article.cleaned_text[:250], 'image': article.top_image.src}
return jsonify(response)
if __name__ == "__main__":
app.run(debug=True)

Running background Celery task in Flask

Problem has been updated to include progress made
I have the following code and my celery tasks kick off fine, I just don't know where I should store the async result so that I can look at it again later
#!/usr/bin/env python
"""Page views."""
from flask import render_template, request
from flask import Flask
from celerytest import add
from time import sleep
app = Flask(__name__)
async_res = []
#app.route('/', methods=['GET', 'POST'])
def run():
if request.method == 'GET':
return render_template("template.html")
else:
form = request.form
n1 = str(form.get("n1"))
n2 = str(form.get("n2"))
aysnc_res = add.delay(n1,n2)
return render_template("loading.html")
#app.route('/loading')
def check_if_complete():
if async_res.ready() == True:
return render_template("template2.html", val=async_res.get())
else:
sleep(5)
return render_template("loading.html")
if __name__ == '__main__':
app.run()
It appears that storing async_res as a global variable in my flask app causes server errors. So what's the best method of storing the result so that I can check on it in my "loading page"
I ended up being able to save the ID of my task in the session dictionary in Flask
See code below:
#!/usr/bin/env python
"""Page views."""
from flask import render_template, request
from flask import Flask
from celerytest import add
from time import sleep
app = Flask(__name__)
#app.route('/', methods=['GET', 'POST'])
def run():
if request.method == 'GET':
return render_template("template.html")
else:
form = request.form
n1 = str(form.get("n1"))
n2 = str(form.get("n2"))
aysnc_res = add.delay(n1,n2)
session['TASK_ID'] = async_res.id
return render_template("loading.html")
#app.route('/loading')
def check_if_complete():
aysnc_res = session['TASK_ID']
if async_res.ready() == True:
return render_template("template2.html", val=async_res.get())
else:
sleep(5)
return render_template("loading.html")
if __name__ == '__main__':
app.run()

Displaying stackOverflow API JSON data using Flask

I've been trying to display my username and reputation from the JSON data retrieved from the StackOverflow API.
Im using the python module Requests to retrieve the data.
Here is the code
from flask import Flask,jsonify
import requests
import simplejson
import json
app = Flask(__name__)
#app.route("/")
def home():
uri = "https://api.stackexchange.com/2.0/users? order=desc&sort=reputation&inname=fuchida&site=stackoverflow"
try:
uResponse = requests.get(uri)
except requests.ConnectionError:
return "Connection Error"
Jresponse = uResponse.text
return Jresponse
if __name__ == "__main__":
app.run(debug = True)
The unused imports is what I need to get this done but cant seem to know how to get it done.
Below is what is returned to the browser, I want to just display the username [display_name] and the reputation. what options do I have to get this done ?
{"items":[{"user_id":540028,"user_type":"registered","creation_date":1292207782,"display_name":"Fuchida","profile_image":"http://www.gravatar.com/avatar/6842025a595825e2de75dfc3058f0bee?d=identicon&r=PG","reputation":13,"reputation_change_day":0,"reputation_change_week":0,"reputation_change_month":0,"reputation_change_quarter":0,"reputation_change_year":0,"age":24,"last_access_date":1332905685,"last_modified_date":1332302766,"is_employee":false,"link":"http://stackoverflow.com/users/540028/fuchida","website_url":"http://blog.Fuchida.me","location":"Minneapolis
MN","account_id":258084,"badge_counts":{"gold":0,"silver":0,"bronze":3}}],"quota_remaining":282,"quota_max":300,"has_more":false}
Use json.loads() to read and decode the data.
from flask import Flask,jsonify
import requests
import simplejson
import json
app = Flask(__name__)
#app.route("/")
def home():
uri = "https://api.stackexchange.com/2.0/users? order=desc&sort=reputation&inname=fuchida&site=stackoverflow"
try:
uResponse = requests.get(uri)
except requests.ConnectionError:
return "Connection Error"
Jresponse = uResponse.text
data = json.loads(Jresponse)
displayName = data['items'][0]['display_name']# <-- The display name
reputation = data['items'][0]['reputation']# <-- The reputation
return Jresponse
if __name__ == "__main__":
app.run(debug = True)

Categories

Resources