Reputation: 386
I'm using Flask to write a very basic API. All it should do is get the string from the URI, convert it to an integer, multiply it by 1000 to go from seconds to milliseconds, add it to the current time in milliseconds, and parse the output in time format.
Here is the code I have so far for converting seconds to milliseconds:
from flask import Flask, request, url_for
@app.route('/api/<seconds>')
def api_seconds(seconds):
milliseconds = int(seconds) * 1000
return 'Seconds: ' + seconds + '\n' + 'Milliseconds: ' + milliseconds
This returns an Internal Server Error. When I remove the milliseconds variable completely and just use seconds, it works fine. According to the Flask page, @app.route('api/<int:seconds>')
should return the string as an integer and I could just omit the int(seconds)
. However, this also returns an Internal Server Error.
Also, '\n'
isn't creating a new line for me.
Upvotes: 0
Views: 767
Reputation: 49318
You can't concatenate strings with integers. Convert the integer to a string before concatenation.
return 'Seconds: ' + seconds + '\n' + 'Milliseconds: ' + str(milliseconds)
^
Alternatively, you can use string formatting, which takes care of that for you:
return 'Seconds: {}\nMilliseconds: {}'.format(seconds, milliseconds)
Since you're working with the Flask framework and will be displaying this as HTML in a browser, you'll need to use an appropriate line break like <br />
:
return 'Seconds: {}<br />Milliseconds: {}'.format(seconds, milliseconds)
Upvotes: 3