I am learning about python and i need to send some data that i got from twitter api using tweepy.
From python to javascript. I tried using Flask and ajax but i don't know why it does't work
This is my python file i ran it before using flask print(text) and it works perfectly in console, but i need to send them to javascript to display them in html
This is my python file called twitter.py
from flask.typing import AppOrBlueprintKey
import tweepy
from flask import Flask, render_template,jsonify
access_token="token"
access_token_secret="secret"
api_key="key"
api_key_secret="secret"
auth =tweepy.OAuthHandler(consumer_key=api_key,consumer_secret=api_key_secret)
auth.set_access_token(access_token,access_token_secret)
api=tweepy.API(auth,wait_on_rate_limit=True)
hashtag = input("Enter hashtag: ")
tweets = tweepy.Cursor(api.search_tweets,q=hashtag,include_entities=True).items(1)
for tweet in tweets:
text = tweet.text
application = Flask(__name__)
@application.route('/get_tweets',method=['POST'])
def gettweets():
text = tweet.text
return jsonify('',render_template('justTweet.php',x=text))
@application.route('/')
def homepage():
return render_template('nuevo.php',x=text)
application.run()
HTML WITH JS just to try called nuevo.php
<div id="tweet">
{{x}}
</div>
<script>
function getTweet(){
$.ajax({
url:"/get_tweets",
type:"POST",
dataType:"json",
success: function(data){
$(justTweet).replaceWith(data)
}
})
}
console.log(getTweet());
</script>
Also i saw i had to create a new file to receive data before sending to main file php or html
justTweet.php
<div id="tweet">
{{x}}
</div>