EzDevInfo.com

flask interview questions

Top flask frequently asked interview questions

Get IP address of visitors using Python + Flask

I'm making a website where users can log on and download files, using the Flask micro-framework (based on Werkzeug) which uses Python (2.6 in my case).

I need to get the IP address of users when they log on (for logging purposes). Does anyone know how to do this? Surely there is a way to do it with Python?


Source: (StackOverflow)

Python - Flask or django for a beginner? [closed]

I want to take the web development path and have a career as a web developer using Python, and really want to learn django because it's a full-stack web framework and will be more beneficial for me when looking for a job as a web developer.

But a friend told me to begin with flask as it's simpler, and will help me when i start learning django, as django is more complicated than flask. So flask will ease up things for me when i start learning django.

So should i start with flask, knowing that i'll eventually learn django ? Or just begin learning django ?


Source: (StackOverflow)

Advertisements

Does Flask support regular expressions in its URL routing?

I understand that Flask has the int, float and path converters, but the application we're developing has more complex patterns in its URLs.

Is there a way we can use regular expressions, as in Django?


Source: (StackOverflow)

ImportError: No Module Named bs4 (BeautifulSoup)

I'm working in Python and using Flask. When I run my main Python file on my computer, it works perfectly, but when I activate venv and run the Flask Python file in the terminal, it says that my main Python file has "No Module Named bs4." Any comments or advice is greatly appreciated.


Source: (StackOverflow)

Python Flask, how to set content type

I am using Flask and I return an XML file from a get request. How do I set the content type?

e.g.

@app.route('/ajax_ddl')
def ajax_ddl():
    xml = 'foo'
    header("Content-type: text/xml")
    return xml

Source: (StackOverflow)

Can I serve multiple clients using just Flask app.run() as standalone?

I know I can link Flask with Apache or other web servers. But, I was thinking of running Flask as a standalone server serving multiple clients simultaneously.

Is this possible? Do I have to handle spawning multiple threads and managing them?


Source: (StackOverflow)

Create dynamic URLs in Flask with url_for()

Half of my Flask routes requires a variable say, /<variable>/add or /<variable>/remove. How do I create links to those locations?

url_for() takes one argument for the function to route to but I can't add arguments?


Source: (StackOverflow)

flask - how do you get a query string from flask

Not obvious from the flask documention on how to get the query string. I am new, looked at the docs, could not find!

So

@app.route('/')
@app.route('/data')
def data():
    query_string=??????
    return render_template("data.html")

Source: (StackOverflow)

Flask Optional URL parameters

Is it possible to directly declare a flask url optional parameter, currently I'm proceeding the following way:

@user.route('/<userId>')
@user.route('/<userId>/<username>')
def show(userId,username=None):
    .................

is there anything that can allow me to directly say that "username" is optional?


Source: (StackOverflow)

How do I get the user agent with Flask?

I'm trying to get access to the user agent with Flask, but I either can't find the documentation on it, or it doesn't tell me.


Source: (StackOverflow)

Python Flask vs Bottle [closed]

What are the large distinctions between these two microframeworks? It seems Bottle is more flexible in terms of the templating engine and other configurations, but flask supports many useful plugins like flask-openID.

How are they fundamentally different, and why have they not merged?


Source: (StackOverflow)

Flask - configure dev server to be visible across the network

I'm not sure if this is Flask specific, but when I run an app in dev mode (http://localhost:5000), I cannot access it from other machines on the network (with http://[dev-host-ip]:5000). With Rails in dev mode, for example, it works fine. I couldn't find any docs regarding the Flask dev server configuration. Any idea what should be configured to enable this?


Source: (StackOverflow)

json.dumps vs flask.jsonify

I am not sure I understand the purpose of flask.jsonify method. I try to make json string from this:

data = {"id": str(album.id), "title": album.title}

but what I get with json.dumps differs from what I get with flask.jsonify.

json.dumps(data): [{"id": "4ea856fd6506ae0db42702dd", "title": "Business"}]
flask.jsonify(data): {"id", "title"}

Obviously I need to get result that looks more like what json.dumps returns. What am I doing wrong?


Source: (StackOverflow)

Typical Angular.js workflow and project structure (with Python Flask)

I am pretty new to this whole MV* client-side framework frenzy. It doesn't have to be Angular.js, but I picked it because it feels more natural to me than either Knockout, Ember or Backbone. Anyway what is the workflow like? Do people start with developing a client-side application in Angular.js and then hooking up the back-end to it? Or the other way around by first building the back-end in Django, Flask, Rails and then attaching an Angular.js app to it? Is there a "right" way of doing it, or is it just a personal preference in the end?

I am also not sure whether to structure my project according to the Flask or Angular.js? community practices.

For example, Flask's minitwit app is structured like so:

minitwit
|-- minitwit.py
|-- static
   |-- css, js, images, etc...
`-- templates
   |-- html files and base layout

Angular.js tutorial app is structured like this:

angular-phonecat
|-- app
    `-- css
    `-- img
    `-- js
    `-- lib
    `-- partials
    `-- index.html
|-- scripts
 `-- node.js server and test server files

I could picture a Flask app by itself, and it's fairly easy to see Angular.js app like ToDo List by itself but when it comes to using both of these technologies I don't understand how they work together. It almost seems like I don't need a server-side web-framework when you already have Angular.js, a simple Python web server will suffice. In the Angular to-do app for example they use MongoLab to talk to the database using Restful API. There was no need having a web framework on the back-end.

Maybe I am just awfully confused, and Angular.js is nothing more than a fancy jQuery library so I should use just like I would use jQuery in my Flask projects (assuming I change Angular's template syntax to something that doesn't conflict with Jinja2). I hope my questions make some sense. I mainly work on the back-end and this client-side framework is an unknown territory for me.


Source: (StackOverflow)

How to organize a relatively large Flask application?

I'm building my first Flask app and I can't figure out a good, clean Pythonic way of organizing my application. I don't want to have everything in a single .py file as in their example. I would like to have each part of my app in a separate module. What would be a good way to organize things?


Source: (StackOverflow)