简体   繁体   English

使用 ReactJS 和 python Flask。 一般的做法

[英]Using ReactJS and python Flask. General Approach

I am working on project, that I started with Python and Flask, as at the begginning it was quite small - the jinja templates commonly used by Flask was sufficient, but as I wanted more and more - I decided to give a try to ReactJS.我正在研究项目,我从 Python 和 Flask 开始,因为在开始时它很小 - Flask 常用的 jinja 模板就足够了,但是随着我想要的越来越多 - 我决定尝试 ReactJS。

It works great for variety of things, and there is bigger control of whole frontend, and generally I like it, but Let's assume that I want to use react for X component, and in the same X component i'd like to use jinja templates - to populate it with data from Flask backend.它适用于各种事情,并且对整个前端有更大的控制,通常我喜欢它,但是假设我想对 X 组件使用 react,并且在同一个 X 组件中我想使用 jinja 模板- 用来自 Flask 后端的数据填充它。 This is the first question - 1) How should be this achieved?这是第一个问题 - 1)这应该如何实现? React is preparing javascript file, so the tempalte can't be rendered. React 正在准备 javascript 文件,因此无法呈现模板。 Should I create some endpoint, and GET data needed to populate with jquery request?我应该创建一些端点,并获取需要用 jquery 请求填充的数据吗?

The second question is - the routing.第二个问题是——路由。 I define some endpoints in flask - but in react components I need to 'hardcode' it.我在烧瓶中定义了一些端点 - 但在反应组件中我需要对其进行“硬编码”。 Is there some solution to not have it hardcoded in 2 places?是否有一些解决方案可以不将其硬编码在 2 个地方?

The third question is - how do you work with that?第三个问题是 - 你如何处理它? Maybe good point is to combine React for some components, and use tempaltes for other things?也许好的一点是将 React 用于某些组件,而将模板用于其他事物?

Summarizing question:总结问题:

  1. How to inject flask data into React components.如何将烧瓶数据注入 React 组件。
  2. How to face routing.如何面对路由。
  3. General points of working with React + Flask / Django.使用 React + Flask / Django 的一般要点。 Your opinion/thoughts/guides.您的意见/想法/指南。

I am very interested in your opinions about this two frameworks working together :)我对你对这两个框架协同工作的看法很感兴趣:)

  1. You can make two separate apps, one with flask (endpoints serving json data) and one with react (a frontend app, that requests this data).您可以制作两个单独的应用程序,一个使用flask(端点提供json 数据),一个使用react(一个前端应用程序,请求这些数据)。 You can use axios in react to make requests to your api.您可以在反应中使用axios向您的 api 发出请求。 For example you write a class/component for that purpose and use that to request data in a few lines of code in Your other components.例如,您为此目的编写了一个类/组件,并使用它在其他组件中的几行代码中请求数据。 If jinja You mentioned is a templating engine I don't think there is an easy way to use that with react.如果你提到的 jinja 是一个模板引擎,我认为没有一种简单的方法可以将它与 react 一起使用。
  2. In React You usually develop Single Page Applications, but You can use react-router in order to route your subpages.在 React 中,您通常会开发单页应用程序,但是您可以使用react-router来路由您的子页面。 If I understand correctly this is a situation You are talking about: In react You write a component that is responsible for displaying a list of articles.如果我理解正确,这是您正在谈论的一种情况:在 react 中,您编写了一个组件,负责显示文章列表。 In that component You 'hardcode' a function using (using axios for example) that download the list in json format.在该组件中,您使用(例如使用 axios)“硬编码”一个函数,该函数以 json 格式下载列表。 Later You do the same for a component that displays a specific article.稍后您对显示特定文章的组件执行相同操作。
  3. I think it's easier to be consistent and develop a SPA application.我认为保持一致并开发 SPA 应用程序更容易。 Especially when You have things like react-router, axios or redux already set up.特别是当您已经设置了 react-router、axios 或 redux 之类的东西时。 I'm a beginner ( don't be harsh on me (: ), but I think these frameworks should work with each other just fine. I recommend this udemy course . I learned there a lot about react.我是初学者(不要对我太苛刻 (:) ,但我认为这些框架应该可以很好地相互配合。我推荐这门 udemy 课程。我在那里学到了很多关于React 的知识。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM