cancel
Showing results for 
Search instead for 
Did you mean: 

Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.

Error with params using python neo4j driver and flask app

olivier
Node Link

Hello, I'm newbie here

I'm trying to write a little web app with neo4j 4.0 python 3.8 and flask.
I followed the doc here https://neo4j.com/docs/driver-manual/current/session-api/simple/#driver-simple-transaction-fn

when I execute my python code directly in the python shell I get the expected result
when I execute it from the web page I get the error **TypeError: The view function did not return a valid response. The function either returned None or ended without a return statement.
**

My code is very simple :

def search_news_by_name(tx, name):
	query = """
		MATCH (n:News {Symbol: $name}) 
		RETURN n.Date, n.Symbol, n.Title 
		ORDER BY n.Date DESC
		LIMIT 25
		"""
	print(query, name, (tx.run(query, name=name)).__dict__)
	newsList = []
	for news in tx.run(query, name=name):
		newsList.append({'Date': news['n.Date'],'Symbol': news['n.Symbol'],'Title': news['n.Title']})
	return newsList		

def search_query(name):
	name='UBI'
	driver = GraphDatabase.driver("neo4j://server.dscl.fr:7687", auth=("neo4j", "s56brApr"))	
	with driver.session() as session:
		newsList = session.read_transaction(search_news_by_name, name)
	driver.close()
	return (newsList)

and from the Flask App :

@app.route('/search', methods = ['GET', 'POST'])
def search():

    if request.method == 'POST':
        my_data = search_query(request.form.get('search'))

The full debug trace gives :

 * Debugger PIN: 339-966-619

query=	MATCH (n:News {Symbol: $name}) 
		RETURN n.Date, n.Symbol, n.Title 
		ORDER BY n.Date DESC
		LIMIT 25

name=	UBI 

{'_connection': <neo4j.io._bolt4x0.Bolt4x0 object at 0x7fe008bab880>, '_hydrant': <neo4j.data.DataHydrator object at 0x7fe008baba60>, '_on_closed': <bound method Transaction._result_on_closed_handler of <neo4j.work.transaction.Transaction object at 0x7fe008bab850>>, '_metadata': {'query': '\n\t\tMATCH (n:News {Symbol: $name}) \n\t\tRETURN n.Date, n.Symbol, n.Title \n\t\tORDER BY n.Date DESC\n\t\tLIMIT 25\n\t\t', 'parameters': {'name': 'UBI'}, 'server': <neo4j.api.ServerInfo object at 0x7fe008baba00>, 't_first': 0, 'fields': ['n.Date', 'n.Symbol', 'n.Title'], 'qid': 0}, '_record_buffer': deque([]), '_summary': None, '_bookmark': None, '_qid': 0, '_fetch_size': 1000, '_discarding': False, '_attached': True, '_streaming': False, '_has_more': False, '_closed': False, '_keys': ['n.Date', 'n.Symbol', 'n.Title']}
92.154.43.222 - - [23/Jun/2020 09:16:31] "POST /search HTTP/1.1" 500 -
Traceback (most recent call last):
  File "/usr/local/lib/python3.8/dist-packages/flask/app.py", line 2464, in __call__
    return self.wsgi_app(environ, start_response)
  File "/usr/local/lib/python3.8/dist-packages/flask/app.py", line 2450, in wsgi_app
    response = self.handle_exception(e)
  File "/usr/local/lib/python3.8/dist-packages/flask/app.py", line 1867, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/usr/local/lib/python3.8/dist-packages/flask/_compat.py", line 39, in reraise
    raise value
  File "/usr/local/lib/python3.8/dist-packages/flask/app.py", line 2447, in wsgi_app
    response = self.full_dispatch_request()
  File "/usr/local/lib/python3.8/dist-packages/flask/app.py", line 1953, in full_dispatch_request
    return self.finalize_request(rv)
  File "/usr/local/lib/python3.8/dist-packages/flask/app.py", line 1968, in finalize_request
    response = self.make_response(rv)
  File "/usr/local/lib/python3.8/dist-packages/flask/app.py", line 2097, in make_response
    raise TypeError(
TypeError: The view function did not return a valid response. The function either returned None or ended without a return statement.

Any help will be appreciated

2 REPLIES 2

Hello @olivier

Add a return statement to your search endpoint:

@app.route('/search', methods = ['GET', 'POST'])
def search():
    if request.method == 'POST':
        my_data = search_query(request.form.get('search'))
    return my_data

Regards,
Cobra

jggomez
Graph Voyager

Only for now .... return my_data