2

i have a sentiment analysis python script, but I want to make it runs in html and get the input form from html page, and the shows the result back in html page. I already use a django framework to run html page. But i don't know how to connect it with python script I have.

this is my python script

query = input("query? \n")
number = input("number of tweets? \n")

results = api.search(
   lang="en",
   q=query + " -rt",
   count=number,
   result_type="recent"
)

print("--- Gathered Tweets \n")

## open a csv file to store the Tweets and their sentiment
file_name = 'Sentiment_Analysis_of_{}_Tweets_About_{}.csv'.format(number, query)

with open(file_name, 'w', newline='') as csvfile:
    csv_writer = csv.DictWriter(
        f=csvfile,
        fieldnames=["Tweet", "Sentiment"]
    )
csv_writer.writeheader()

print("--- Opened a CSV file to store the results of your sentiment analysis... \n")

## tidy up the Tweets and send each to the AYLIEN Text API
   for c, result in enumerate(results, start=1):
      tweet = result.text
      tidy_tweet = tweet.strip().encode('ascii', 'ignore')

      if len(tweet) == 0:
          print('Empty Tweet')
          continue

      response = client.Sentiment({'text': tidy_tweet})
      csv_writer.writerow({
          'Tweet': response['text'],
          'Sentiment': response['polarity']
      })

      print("Analyzed Tweet {}".format(c))

## count the data in the Sentiment column of the CSV file
with open(file_name, 'r') as data:
   counter = Counter()
   for row in csv.DictReader(data):
      counter[row['Sentiment']] += 1

   positive = counter['positive']
   negative = counter['negative']
   neutral = counter['neutral']

## declare the variables for the pie chart, using the Counter variables for 
"sizes"
colors = ['limegreen', 'dodgerblue', 'darkorchid']
sizes = [positive, negative, neutral]
labels = 'Positif', 'Negatif', 'Netral'
explode = (0.1, 0, 0)

## use matplotlib to plot the chart
plt.pie(
    x=sizes,
    shadow=False,
    colors=colors,
    labels=labels,
    startangle=90,
    explode=explode
)
plt.axis('equal')
plt.title("Sentiment of {} Tweets about {}".format(number, query))
plt.show()
1

1 Answer 1

1

In your template file you can have text field

        <form method="POST" action="/labdashboard/usergivenlab/">
        {% csrf_token %}
            <input type="text" name="textfield" >
            <button type="submit">Submit</button>
        </form>

then in your urls.py define the view

url(r'usergivenlab/$',views.usergivenlab,name='usergivenlab')

And in your views.py you can get the use input using the POST method

usergivenip = request.POST.get('textfield', None)

Sign up to request clarification or add additional context in comments.

1 Comment

Are there any security risks which one should be aware of, when using such an input form? I read a lot of comments about the "danger" of getting exploited, but I cannot see how a simple input like this could be a problem. As I understand, the input text is just saved in a variable and then called within the python script.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.