Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

167
Visualizações
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
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda