How to insert create-react-app project into existing python/flask app - python

We have an existing python/flask app (made by someone else) that has a nav bar, sidebar and different pages. We have built a create-react-app project that we want to sit on one of these pages in the python/flask app.
The question is - how can we integrate our react app into the existing python/flask codebase?
Any help gratefully received!
Update: from helpful answer below - could anyone help with explaining this a little more - perhaps with code examples? "render a container in the same template that react can mount on."
Update 2: Someone else suggested "You can have React run anywhere where you can add a script tag into HTML and a DOM element to attach it to. As long as you have a way to bundle and serve the JS files, you should just be able to include your new app like that." - could anybody help with code examples of this?

A rough outline would be:
configure the build folder to be the folder where static files are served from by your server (js, css, ...)
include the files from that folder in the template of the page your want your react app to display (via <link> and <script> tags).
render a container in the same template that react can mount on.

Related

How to host deploy my django project to firebase

I made this little django project, it shows weather of next three days of given city, its just a single page project, it looks like this
i want to deploy/host it on firebase
my project link here
But i have no idea how to do it, please help.
Edit
Ok, now i know that i can use cloud run for my backend and firebase for my frontend, can someone give me step by step procedure how to put my django files in cloudrun and firebase, and how to connect them, please
Firebase Hosting only hosts static content, which means it doesn't run your Python/Django code. But you can run the code on Cloud Run, and then integrate that with Firebase Hosting. See https://firebase.google.com/docs/hosting/cloud-run
You can upload dynamic or static content on firebase, in the case of a Django app it's dynamic but your stylesheets / scripts are static content.
In settings.py you have to specify a route for your static files and store them in, like : STATIC_ROOT = '/path/to/static'
Then in your server you have to specify that all the static files are stored in the above path.
Find more informations here : https://cloud.google.com/appengine/docs/standard/python3/serving-static-files

How to host Jekyll and static HTML sites under a slash of a Flask Jinja2 site?

Okay, what I want to do seems simple but isn't. Here is what I have and want:
A website created with Flask and Jinja 2: https://www.neuraxio.com/
A documentation website for an open-source library created with sphinx. The output is raw static HTML files: https://www.neuraxle.neuraxio.com/stable/index.html However, I would like to move this website to https://www.neuraxio.com/neuraxle/stable/index.html with redirects from the old subdomain to the new one. The static HTML site is hosted with github pages for now.
I want to create a jekyll blog with github pages because it's simple to manage and create new articles from markdown. Or perhaps a Wordpress blog, but something. I'd like to host that blog under https://www.neuraxio.com/blog/ and not under https://www.blog.neuraxio.com/
I know subdomains are easy to manage from a DNS and hosting standpoint, but I'd like to centralize everything under the same subdomain: www.neuraxio.com.
So, how would you approach solving this problem?
For the static HTML of part 2, it might be as simple as moving the static HTML to a subfolder of the public static HTML of the Jinja 2 theme, except the fact that it might break relative links (?).
For the Jekyll or Wordpress site, I'd like it hosted under a slash of the domain and not a subdomain. How would you approach that?
Thank you for suggesting fixes and various creative ways to approach the problem. If that can be solved simply with the existing Flask and Jinja 2 code, that'd be cool. Or by using strange DNS tricks considering I'm already set up with the hosting of each subsites.
I expect to have relative HTML links errors, however. How should I prepare agains that, too, or automatically fix each link?
I suggest adding a CNAME dns record that can point https://www.neuraxle.neuraxio.com/stable/index.html
to
https://www.neuraxio.com/neuraxle/stable/index.html
This is pretty easy to do thats why I would have just gave you a link if I could but I will write a long answer instead :
1 - Log in to your DNS management service (GoDaddy.com, googlecloud dns service...) For my part, my website is hosted on google cloud DNS.
2- Click on your website.
3- Select Add a Record Set
4- Select CNAME record
5- I the DNS name form add https://www.neuraxle.neuraxio.com/
6- In the canonical name (the alias) add : https://www.neuraxio.com/neuraxle/
I wouldnt put this part /stable/index.html because your server should be abble to take care of pointing to it.
Hope this helps !

AirBnB knowledge repo: Deploying using a context route

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.

using heroku to deploy an app? Basic queries

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.

Can you serve static HTML pages from Pyramid?

I have a Pyramid app using Mako templates and am wondering if it is possible to serve static HTML pages within the app?
For the project I'm working on, we want to have relatively static pages for the public "front-facing" bits, and then the application will dynamically serve the meat of the site. We would like one of our internal users to be able to edit some of the HTML content for these pages to update them.
I have my static folder that I'm serving CSS and scripts from, but that doesn't seem to really fit what I'd like to do. I could create views for the pages and basically have static content in the mako templates themselves but I think the application would need to be restarted if someone were to update the template for the changes to appear? Maybe that's not the case?
Long term I would probably do something like store the content in a db and have it dynamically served but that's outside of the scope at this time.
Is there a reasonable way to accomplish this or should I not even bother and set up the public pages as just a regular static HTML site and just link to my app altogether?
Thanks!
You can serve static html from Pyramid by using views that return pre-fabricated responses. You'll have a more fun time doing it though by just having your web server serve static html if it finds it, otherwise proxying the request to your Pyramid app.

Categories

Resources