Milo Chen
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights
    • Engagement control
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Versions and GitHub Sync Note Insights Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       owned this note    owned this note      
    Published Linked with GitHub
    1
    Subscribed
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    Subscribe
    - 本筆演講內容在此連結 https://hackmd.io/dk91-0LyTqe0MaqA8TeoKg - Miro 報到的連結 https://miro.com/app/board/uXjVM2ue-oE=/?share_link_id=483117069515 - 另設有共同編輯區,大家可點此進去編輯打字 https://hackmd.io/@milochen0418/Hy9Byz3Fn - 以前分享過Pynecone (Reflex) 的[雜記](https://hackmd.io/@milochen0418/pynecone-post-python-taiwan) ------- 本篇架構 ------- [TOC] # Reflex 的開發 可以使用 VSCode+conda, VSCode+poetry 等方式來開發,或是初次使用的話可以考慮 [Pynecone (Reflex) Colab-IDE](https://github.com/milochen0418/pynecone-colab-ide) [Pynecone](https://pynecone.app/) 改名至 [Reflex](https://reflex.dev/) ## What is pynecone(Reflex) Pinecone -> Pynecone 松果 -> Reflex pure pyhon 寫 全端網站 (後端、前端) - ReactJS PyQt flask + jQuery MySQL , PostgreSQL ReactJS .NET framework 前端 React 2023 Q1 top 1 最熱的開源新創項目 YC seed https://github.com/pynecone-io/pynecone ## Reflex 架構簡介 很簡化的來說,就是透過開發團隊的努力,將FastAPI + React 的集成,最終包出一個簡易好用的WebApp框架給你用, 希望讓人們可以用純python 來發展WebApp ### 後端的部份 簡單來說就是 FastAPI ,但是實戰app開發中,它框架已經作了簡化,因此你不用處理到中間複雜的溝通過程。 ### 前端的部份 前端使用 ReactJS (或是說NextJS) 在作畫面調整的部份部份,支持 [TailwindCSS](https://tailwindcss.com/), [ChakraUI](https://chakra-ui.com/),而且另一個特好用的優點,你可以自己使用其它React元件 來擴充。Reflex願景是希望可以盡可能將React的優點拿來使用。 目前,Netflix、Uber、星巴克或 Twitch 使用了NextJS NextJS是基於React的框架,差別在於 ReactJS 是在 browser 上直接作了渲染,而 Next.js將渲染方面的工作移到了服務器上,因此客戶端不需要處理這些信息。這提高了性能和SEO,因為服務器預先渲染頁面,然後將最終的HTML發送給客戶端,從而使JS最小化,也就是說加載的代碼更少。不僅用戶可以期待一個更快的網站,而且爬蟲也可以更容易地看到你的網站,並對其進行相應的索引。 Pynecone的渲染到底是 NextJS的哪種呢? NextJS它支持三種渲染方式包括 客戶端渲染 BSR (Broswer Side Render), 靜態頁面生成 SSG (Static Site Generation), 服務端渲染 SSR (Server Side Render) 而 [pynecone 使用 SSG](https://news.ycombinator.com/item?id=35137402) 靠 [bun-runtime](https://bun.sh/) 來作,因為渲染效能比 NodeJS 快約三倍以上。 [其餘渲染知識請見此](https://zhuanlan.zhihu.com/p/341229054) # 實戰開發 ## 申請 key 的部份 ### ngrok auth token ### openai key ## 開始開發 讓我們來寫寫看一個 DALL-E ### 建立初始專案 ```bash= mkdir mydalle cd mydalle reflex init reflex run ``` ### 新增requirments.txt (Optional) ```txt= reflex==0.2.0 openai ``` ### 簡化成 HelloWorld ```python= """Welcome to Reflex! This file outlines the steps to create a basic app.""" import reflex as rx import openai import os class State(rx.State): """The app state.""" pass def index() -> rx.Component: return rx.center( rx.heading("Hello World"), ) # Add state and page to the app. app = rx.App(state=State) app.add_page(index, title="MyDALL-E") app.compile() ``` ### Layout ```python= import reflex as rx import openai import os openai.api_key = os.environ.get("OPENAI_KEY","YOUR_OPENAI_KEY") class State(rx.State): """The app state.""" image_url = "" image_processing = False image_made = False def get_dalle_result(self, form_data: dict[str, str]): pass pass def index() -> rx.Component: return rx.center( rx.vstack( rx.heading("DALL-E", font_size="1.5em"), rx.form( rx.input(id="prompt_text", placeholder="Enter a prompt.."), rx.button( "Generate Image", type_="submit", width="100%", ), on_submit=State.get_dalle_result, ), rx.divider(), rx.cond( State.image_processing, rx.circular_progress(is_indeterminate=True), rx.cond( State.image_made, rx.image( src=State.image_url, height="25em", width="25em", ), ), ), bg="white", padding="2em", shadow="lg", border_radius="lg", ), width="100%", height="100vh", background="radial-gradient(circle at 22% 11%,rgba(62, 180, 137,.20),hsla(0,0%,100%,0) 19%),radial-gradient(circle at 82% 25%,rgba(33,150,243,.18),hsla(0,0%,100%,0) 35%),radial-gradient(circle at 25% 61%,rgba(250, 128, 114, .28),hsla(0,0%,100%,0) 55%)", ) # Add state and page to the app. app = rx.App(state=State) app.add_page(index, title="MyDALL-E App") app.compile() ``` ### 完成 ```python= import reflex as rx import openai import os openai.api_key = os.environ.get("OPENAI_KEY","YOUR_OPENAI_KEY") class State(rx.State): """The app state.""" image_url = "" image_processing = False image_made = False def get_dalle_result(self, form_data: dict[str, str]): prompt_text:str = form_data['prompt_text'] self.image_processing = True yield try: response = openai.Image.create(prompt=prompt_text, n=1, size="1024x1024") self.image_url = response["data"][0]["url"] self.image_processing = False self.image_made = True yield except: self.image_processing = False yield rx.window_alert("Error to call openAI API") pass def index() -> rx.Component: return rx.center( rx.vstack( rx.heading("DALL-E", font_size="1.5em"), rx.form( rx.input(id="prompt_text", placeholder="Enter a prompt.."), rx.button( "Generate Image", type_="submit", width="100%", ), on_submit=State.get_dalle_result, ), rx.divider(), ## rx.cond( Treu/False, True:作啥layout, False:作啥layout ) rx.cond( State.image_processing, rx.circular_progress(is_indeterminate=True), rx.cond( State.image_made, rx.image( src=State.image_url, height="25em", width="25em", ), ), ), bg="white", padding="2em", shadow="lg", border_radius="lg", ), width="100%", height="100vh", background="radial-gradient(circle at 22% 11%,rgba(62, 180, 137,.20),hsla(0,0%,100%,0) 19%),radial-gradient(circle at 82% 25%,rgba(33,150,243,.18),hsla(0,0%,100%,0) 35%),radial-gradient(circle at 25% 61%,rgba(250, 128, 114, .28),hsla(0,0%,100%,0) 55%)", ) # Add state and page to the app. app = rx.App(state=State) app.add_page(index, title="MyDALL-E App") app.compile() ``` # 聽眾 碰過 Django, flask, FastAPI, 想評估 reflex

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully