I'm using d3.js for showing our data on map using flask. I'm trying to get the world.json file in here using this @app.route('/', methods=['POST' ]) but it is giving error of 127.0.0.1 - - [29/Apr/2019 14:33:40] "POST / HTTP/1.1" 200 - and 127.0.0.1 - - [29/Apr/2019 14:33:41] "GET /world.json HTTP/1.1" 404 -. I have stored my world.json file in temp folder.

import io
import csv
import pandas as pd
import numpy as np
from flask_googlemaps import GoogleMaps
from flask_googlemaps import Map,icons
import folium
import numpy as np
from sklearn.metrics.pairwise import cosine_similarity

########################################################################################################3

#from __future__ import division
import string
import math
import pandas

def kuch(doc_headline):
    import os
    import pandas as pd
    import numpy as np
    from nltk.tokenize import word_tokenize
    from gensim import corpora, models, similarities

    # This one is good when files of news corpus dict matrix are already generated and we are using notebook
    # Use this saved matrix starategy in tfidf search in web app
    if (os.path.exists("adminWorkOutput/news.dict")):
        dictionary = corpora.Dictionary.load('adminWorkOutput/news.dict')
        corpus = corpora.MmCorpus('adminWorkOutput/news.mm')
        print("Already generated files are Used")
    else:
        print("Fresh run to generate saved dict matrix data ")

    tfidf = models.TfidfModel(corpus)
    corpus_tfidf = tfidf[corpus]

    # one
    # for doc in corpus_tfidf:
    #    print(doc)
    # two
    for doc in corpus_tfidf:
        doc = [(dictionary.get(idx), tfidf) for idx, tfidf in doc]
        #print(doc)

    # one way
    index = similarities.MatrixSimilarity(corpus_tfidf)
    # print(np.array(index))

    print("\nxxxxxxxxxxxxxxxxxxxxxxxx Similarities \n")

    # other way
    sims = similarities.Similarity('adminWorkOutput/', corpus_tfidf,
                                   num_features=len(dictionary))
    #print(sims)
    # print(type(sims))

    # documents[819] means news_820
    # because array start from 0 and news start from 1
    qry = doc_headline  # str(documents[819])
    print(qry)
    query_doc = [w.lower() for w in word_tokenize(qry)]
    # print(query_doc)
    query_doc_bow = dictionary.doc2bow(query_doc)
    # print(query_doc_bow)
    query_doc_tf_idf = tfidf[query_doc_bow]
    # print(query_doc_tf_idf)
    print("\nxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\n")
    my_result = sims[query_doc_tf_idf]
    #print(my_result)

    # print(list(enumerate(my_result)))
    arr = sorted(enumerate(my_result), key=lambda item: -item[1])

    print('this is there\n', arr[1])
    df = pd.read_csv('adminWorkOutput/preprocessed.csv')
    #print(df.loc[arr[1][0], 'topic'])

    records = []
    i = 0
    import random
    while (arr[i][1] > 0.1):
        topic1 = df.loc[arr[i][0], 'headline']
        country1 = df.loc[arr[i][0], 'country']
        similarity_score1 = arr[i][1]
        polarity_score1 = random.uniform(-1, 1)
        records.append((topic1, country1, similarity_score1, polarity_score1))
        i += 1

    df1 = pd.DataFrame(records, columns=['topic', 'country', 'similarity_score', 'polarity_score'])

    df1.to_csv('adminWorkOutput/similar.csv')

    import csv, json
    csvFilePath = "adminWorkOutput/similar.csv"
    jsonFilePath = "adminWorkOutput/file.json"
    arr = []
    # read the csv and add the arr to a arrayn

    with open(csvFilePath) as csvFile:
        csvReader = csv.DictReader(csvFile)
        print(csvReader)
        for csvRow in csvReader:
            arr.append(csvRow)
        # arr = {'Countries': arr} # Added line

    #print(arr)

    # write the data to a json file
    with open(jsonFilePath, "w") as jsonFile:
        jsonFile.write(json.dumps(arr, indent=4))


app = Flask(__name__, static_url_path = "/tmp", static_folder = "tmp")



@app.route('/')
def my_form():
    return render_template('myform2.html')




@app.route('/', methods=['POST' ])
def my_form_post():
    text = request.form['search']

    #processed_text = text.upper()
    print(text)
    kuch(text)
    print("sahi ha")
    return render_template('GeoMap/index.html')


import os
if __name__ == "__main__":
    app.run()

2 Answers

1
ekuusela On

Nothing in your code is mapped to the route GET /world.json.

The static_folder files are available from the static_url_path, try GET /tmp/world.json.

Check out the Flask tutorial, especially the static files section.

1
Tomáš Linhart On

You forgot to provide the content of your template GeoMap/index.html that contains access to the world.json file. In general, you should use url_for to access the file. In your case, where you define your Flask app as

app = Flask(__name__, static_url_path="/tmp", static_folder="tmp")

you should have something like this in your template:

{{ url_for('static', filename='world.json') }}

Thus, this would result in a request to /tmp/world.json URL. Now, it depends what you mean by I have stored my world.json file in temp folder. On Linux/UNIX systems, the temp folder usually is /tmp, but in your app definition, you provide tmp which means tmp folder relative to the project directory. Maybe you wanted to set static_folder="/tmp" then?