skip to Main Content

I’m creating a web app in Python/Flask to display tweets using twitters API using tweepy. I have set up an HTML form, and have got the script that finds the tweets with a certain input, currently, this is hard coded. I want the users to input a hashtag or similar, and then on submit, the script to run, with that as its parameter

I’ve created a form, with method GET and action is to run the script.

<form class="userinput" method="GET" action="Tweepy.py">
    <input type="text" placeholder="Search..">
    <button type="submit"></button>
</form>

I dont know how I would get the users input and store it in a variable to use for the tweepy code, any help would be greatly appreciated

2

Answers


  1. templates/index.html

    <form method="POST">
        <input name="variable">
        <input type="submit">
    </form>
    

    app.py

    from flask import Flask, request, render_template
    app = Flask(__name__)
    
    @app.route('/')
    def my_form():
        return render_template('index.html')
    
    @app.route('/', methods=['POST'])
    def my_form_post():
        variable = request.form['variable']
        return variable
    
    Login or Signup to reply.
  2. I guess you have set up and endpoint to run that script, an URL mapped to a function. In that case you need to call it from the form action.

    for example

    @app.route('/my-function')
    def your_function(parameters):
        "code to do something awesome"
        return "the result of your function"
    

    So, in your form you should:

    <form class="userinput" method="GET" action="/my-function">
        <input type="text" placeholder="Search..">
        <button type="submit"></button>
    </form>
    

    That should do the trick.

    Go to the Flask home page for more examples.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search