Ok, I started with flask and heroku, and I went on to learn more, I am extremely naive as I just started it today. I found this brilliant blog by Ryan Shea,
http://ryaneshea.com/lightweight-python-apps-with-flask-twitter-bootstrap-and-heroku
This particular blog explains the following,
1> what is flask and heroku
2> how to create and write and connect to heroku using CL.
I followed it because it was extremely easy, I had a lot of problems in the middle regarding the ssh keys and creating procfile etc, I used SO to figure it out.
What I did-->
I created virtualenv, then i typed the app.py , created procfile and also the req.txt file.
I connected to heroku and created a stack cedar, renamed it too.
Then comes the HTML files, there is one base.html , then one index and 404 html files.
I typed all of them and saved them. Then When I run the code,
python app.py
The code runs,
I try to connect to the IP from different computer on the same network, it gets connected and it prints
' Hello from python'
Which was the first
#app.route("/")
return 'Hello from python'
My question is, why isnt the base.html or the other html files exist?
I understood how the app was successfully deployed in heroku, but what are the other html files and how do they work?
Please refer to the above link and answer my questions.
I am really naive and do bare it. Thanks a ton.
First, if you are just starting out with Flask I would recommend you read The Flask Mega-Tutorial by Miguel Grinberg. The tutorial goes through the development of a web app, including deployment to Heroku, and does a good job of explaining why and how different methods are used.
Regarding your question, Flask uses .html files as templates using the Jinja 2 template engine, which has a markup syntax similar to Django's. In order to use a template you need to call it with one of Flask's template rendering hooks, most commonly render_template. You can see that render_template is used in the guide you linked here (third from last code block):
#app.route("/")
def index():
return render_template('index.html')
This tells Flask to generate a page using the 'index.html' template located in the templates folder relative to app.py.
Related
This post has to do with deploying AirBnBs knowledge repo app.
We are hosting our knowledge-repo on a sub-route of our server - for example https://aws.our-server-uri.com/knowledge-repo and I'm running into issues with loading static content and redirects.
In summary, I have no way of making the knowledge_repo Flask app aware that the app is running on a sub-route.
The issues
The first issue I ran into was making gunicorn aware of the context route in order to successfully locate the static files, which are now at /knowledge-repo/static/... and not relative to the root url. I solved this by setting the SCRIPT_NAME environment variable to /knowledge-repo before running knowledge_repo --repo . deploy. I'm including this for reference of what I've tried and for potential recommendations of a better solution.
The second issue, which is unresolved, is dynamically prepending our context route to the redirect urls generated by the web app. For example, the Home button in the top navigation bar redirects the user to the root url (https://aws.our-server-uri.com/ in our example case). I need the flask app to be aware of my context route and append /knowledge-repo/ to the page root for all links generated.
What I've tried:
I want to avoid forking and modifying this repo, so I've focussed on ways that do not involve editing the Flask app html, such as setting a <base> tag.
I've set the SCRIPT_NAME environment variable before deploying, but to no avail.
I've played around with setting some variables in a config.py which I passed using the --config config.py flag when running knowledge_repo deploy, but can't seem to find anything that does the trick.
I made some code changes to make it work:
Gave URL prefix to the static content.
Adding URL prefix to the blueprints in flask.
User URL_for with jinja templating to access the routes across HTML and JavaScript.
Here are the code changes.
I had worked with the Flask application which functions as an admin panel and an API. My admin panel includes login page and bunch of admin stuff in it. So I don't want to expose it to the internet.Admin panel should be only accessible from the intranet however my API should be accessible from the internet.
I have two machines.One is a local machine and other one will be hosted at the AWS. The problem is that the code will be same in the two machines however one will serve as an API and other will serve as an Admin panel.
My supervisor told me that I can use "Flask blueprints" to achieve what I am trying to do but I want to be sure before starting to implement.
Can Flask blueprints solve this problem or are there any other options?
(One thing comes to mind is to separate the API from the admin panel into two different Flask apps. Which is easy to do and solves everything. However I am unable to do that right now. )
Image of what I am trying to do
You can use before_app_request on your blueprint and check if your client ip starts with 192.168 or 10.0 or 172.16
from flask import abort
#blueprint_1.before_request
def check_network():
if not request.remote_addr.startswith("192.168") or ...:
abort(404)
Edit : According to Blueprint documentation before_app_request Such a function is executed before each request, even if outside of a blueprint.
use before_request instead (it will be executed before request on blueprint_1 Blueprint ... i edited my code ...
I have a Python program that takes one input (string) and prints out several lines as an output. I was hoping to get it online, so that the web page only has a text-area and a button. Inserting a string and pressing the button should send the string to python and make the original page print out the output from Python.
Assuming I have only dealt with Python and a little bit of HTML and have very limited knowledge about the web, how might I approach this problem?
For some reason, my first instinct was PHP, but Googling "PHP Python application" didn't really help.
That is, assuming what I want IS a 'web application', right? Not a web server, web page or something else?
How could I get a really simple version (like in the first paragraph) up and running, and where to learn more?
Yes, the term you're looking for is "web application". Specifically, you want to host a Python web application.
Exactly what this means, can vary. There are two main types of application - cgi and wsgi (or stand-alone, which is what most people talk about when they say "web app").
Almost nobody uses cgi nowadays, you're much more likely to come across a site using a web application framework, like Django or Flask.
If you're worried about easily making your page accessible to other people, a good option would be to use something like the free version of Heroku do to something simple like this. They have a guide available here:
https://devcenter.heroku.com/articles/getting-started-with-python#introduction
If you're more technically inclined and you're aware of the dangers of opening up your own machine to the world, you could make a simple Flask application that does this and just run it locally:
from flask import Flask, request
app = Flask(__name__)
#app.route('/')
def main():
return 'You could import render_template at the top, and use that here instead'
#app.route('/do_stuff', methods=['POST'])
def do_stuff():
return 'You posted' + request.form.get('your string')
app.run('127.0.0.1', port=5555, debug=True)
Pick a Python web development framework and follow the getting started tutorial. Main options are Django for a "batteries included" approach or Flask for a minimalistic bottom-up approach.
Pardon me, this question is going to be too basic but i have not been able to get the solution anywhere. So, here goes ..
I have set up an instance nginx on Ubuntu 14.04 and an instance of uWSGI on port 9090.
I can direct the traffic from nginx to that one instance of uWSGI, and it works.
But every hello world example i see has something like this
> def application(env, start_response):
> start_response('200 OK', [('Content-Type','text/html')])
> return "Hello World From uWSGI"
and it is just one file... so, if i have multiple python files that needs to be served, distributed through a tree of subdirectories [basically my project]. How can I set that directory structure available for use through HTTP requests?
I would NOT like to use any frame work like Django of flask. But serve simple python file, similar to how a pure html site distributed through subdirectories would serve html.
i might also be missing some concept of how routing can be done. As i feel that has never come up till now.
The directory structure has logic that manipulates a database or interacts with thrid party apis.
Need some guidance to start off.
[Please let me know if there is totally a different way to archive what i am in need of, or if i am on the right track]
Thanks.
The application function is responsible for everything. Of you want to do routing, you do it there, by parsing the request path in env['PATH_INFO'] and then importing and calling the relevant Python functions.
But that would be rather a silly thing to do, seeing is that is exactly what Flask would do for you. You don't need to use anything else from the framework it you really don't want to.
In google App Engine, If I already have a page setup so everything gets redirected to main.py as shown below:
www.foobar.appspot.com/
will always go to main.py
the main.py is setup pretty much the same way as the hello world tutorial (except it calls some more functions that I wrote but that is irrelevant).
Now my question is, how can I set up multiple pages in the format of "Folders" or do anything with urls really... (I have not had much luck with reading through the documentation)
www.foobar.appspot.com/admin/
would go to a class or something, that would obviously handle all of the admin features (for now I would love a hello world). I really do not no, what script I would have that url go to, I am pretty clueless on urls with google App engine. I have seen posts where people had multiple classes in the main.py script but I could not figure out how to change the yaml file to accomidate that.
Your main.py will need to look at the URL and route it to whatever handler function you use.
This is a common task and most web frameworks have some way to handle it. For example, if you're using webapp2:
http://webapp-improved.appspot.com/guide/routing.html
URLs are mapped to python scripts in app.yaml; the first regex you have in there that matches /admin will determine what's executed.
In the Python files themselves, you also need to handle the URLs that are dispatched to them (how you do this depends on what framework you're using; consult your documentation for how to do routing.)