Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

166
Views
How to connect python to javascript properly using Flask AJAX

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>
about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!