timho102003 Wauplin HF Staff commited on
Commit
e74c73c
·
0 Parent(s):

Duplicate from Wauplin/pynecone-on-spaces-template

Browse files

Co-authored-by: Lucain Pouget <Wauplin@users.noreply.huggingface.co>

.dockerignore ADDED
@@ -0,0 +1,139 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Byte-compiled / optimized / DLL files
2
+ __pycache__/
3
+ *.py[cod]
4
+ *$py.class
5
+
6
+ # C extensions
7
+ *.so
8
+
9
+ # Distribution / packaging
10
+ .Python
11
+ build/
12
+ develop-eggs/
13
+ dist/
14
+ downloads/
15
+ eggs/
16
+ .eggs/
17
+ lib64/
18
+ parts/
19
+ sdist/
20
+ var/
21
+ wheels/
22
+ pip-wheel-metadata/
23
+ share/python-wheels/
24
+ *.egg-info/
25
+ .installed.cfg
26
+ *.egg
27
+ MANIFEST
28
+
29
+ # PyInstaller
30
+ # Usually these files are written by a python script from a template
31
+ # before PyInstaller builds the exe, so as to inject date/other infos into it.
32
+ *.manifest
33
+ *.spec
34
+
35
+ # Installer logs
36
+ pip-log.txt
37
+ pip-delete-this-directory.txt
38
+
39
+ # Unit test / coverage reports
40
+ htmlcov/
41
+ .tox/
42
+ .nox/
43
+ .coverage
44
+ .coverage.*
45
+ .cache
46
+ nosetests.xml
47
+ coverage.xml
48
+ *.cover
49
+ *.py,cover
50
+ .hypothesis/
51
+ .pytest_cache/
52
+
53
+ # Translations
54
+ *.mo
55
+ *.pot
56
+
57
+ # Django stuff:
58
+ *.log
59
+ local_settings.py
60
+ db.sqlite3
61
+ db.sqlite3-journal
62
+
63
+ # Flask stuff:
64
+ instance/
65
+ .webassets-cache
66
+
67
+ # Scrapy stuff:
68
+ .scrapy
69
+
70
+ # Sphinx documentation
71
+ docs/_build/
72
+
73
+ # PyBuilder
74
+ target/
75
+
76
+ # Jupyter Notebook
77
+ .ipynb_checkpoints
78
+
79
+ # IPython
80
+ profile_default/
81
+ ipython_config.py
82
+
83
+ # pyenv
84
+ .python-version
85
+
86
+ # pipenv
87
+ # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
88
+ # However, in case of collaboration, if having platform-specific dependencies or dependencies
89
+ # having no cross-platform support, pipenv may install dependencies that don't work, or not
90
+ # install all needed dependencies.
91
+ #Pipfile.lock
92
+
93
+ # PEP 582; used by e.g. github.com/David-OConnor/pyflow
94
+ __pypackages__/
95
+
96
+ # Celery stuff
97
+ celerybeat-schedule
98
+ celerybeat.pid
99
+
100
+ # SageMath parsed files
101
+ *.sage.py
102
+
103
+ # Environments
104
+ .env
105
+ .venv
106
+ .venv*
107
+ env/
108
+ venv/
109
+ ENV/
110
+ env.bak/
111
+ venv.bak/
112
+ .venv*
113
+
114
+ # Spyder project settings
115
+ .spyderproject
116
+ .spyproject
117
+
118
+ # Rope project settings
119
+ .ropeproject
120
+
121
+ # mkdocs documentation
122
+ /site
123
+
124
+ # mypy
125
+ .mypy_cache/
126
+ .dmypy.json
127
+ dmypy.json
128
+
129
+ # Pyre type checker
130
+ .pyre/
131
+ .vscode/
132
+ .idea/
133
+
134
+ .DS_Store
135
+
136
+ # Pynecone
137
+ pynecone.db
138
+ .web
139
+ node_modules
.gitattributes ADDED
@@ -0,0 +1,34 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ *.7z filter=lfs diff=lfs merge=lfs -text
2
+ *.arrow filter=lfs diff=lfs merge=lfs -text
3
+ *.bin filter=lfs diff=lfs merge=lfs -text
4
+ *.bz2 filter=lfs diff=lfs merge=lfs -text
5
+ *.ckpt filter=lfs diff=lfs merge=lfs -text
6
+ *.ftz filter=lfs diff=lfs merge=lfs -text
7
+ *.gz filter=lfs diff=lfs merge=lfs -text
8
+ *.h5 filter=lfs diff=lfs merge=lfs -text
9
+ *.joblib filter=lfs diff=lfs merge=lfs -text
10
+ *.lfs.* filter=lfs diff=lfs merge=lfs -text
11
+ *.mlmodel filter=lfs diff=lfs merge=lfs -text
12
+ *.model filter=lfs diff=lfs merge=lfs -text
13
+ *.msgpack filter=lfs diff=lfs merge=lfs -text
14
+ *.npy filter=lfs diff=lfs merge=lfs -text
15
+ *.npz filter=lfs diff=lfs merge=lfs -text
16
+ *.onnx filter=lfs diff=lfs merge=lfs -text
17
+ *.ot filter=lfs diff=lfs merge=lfs -text
18
+ *.parquet filter=lfs diff=lfs merge=lfs -text
19
+ *.pb filter=lfs diff=lfs merge=lfs -text
20
+ *.pickle filter=lfs diff=lfs merge=lfs -text
21
+ *.pkl filter=lfs diff=lfs merge=lfs -text
22
+ *.pt filter=lfs diff=lfs merge=lfs -text
23
+ *.pth filter=lfs diff=lfs merge=lfs -text
24
+ *.rar filter=lfs diff=lfs merge=lfs -text
25
+ *.safetensors filter=lfs diff=lfs merge=lfs -text
26
+ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
27
+ *.tar.* filter=lfs diff=lfs merge=lfs -text
28
+ *.tflite filter=lfs diff=lfs merge=lfs -text
29
+ *.tgz filter=lfs diff=lfs merge=lfs -text
30
+ *.wasm filter=lfs diff=lfs merge=lfs -text
31
+ *.xz filter=lfs diff=lfs merge=lfs -text
32
+ *.zip filter=lfs diff=lfs merge=lfs -text
33
+ *.zst filter=lfs diff=lfs merge=lfs -text
34
+ *tfevents* filter=lfs diff=lfs merge=lfs -text
.gitignore ADDED
@@ -0,0 +1,139 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Byte-compiled / optimized / DLL files
2
+ __pycache__/
3
+ *.py[cod]
4
+ *$py.class
5
+
6
+ # C extensions
7
+ *.so
8
+
9
+ # Distribution / packaging
10
+ .Python
11
+ build/
12
+ develop-eggs/
13
+ dist/
14
+ downloads/
15
+ eggs/
16
+ .eggs/
17
+ lib64/
18
+ parts/
19
+ sdist/
20
+ var/
21
+ wheels/
22
+ pip-wheel-metadata/
23
+ share/python-wheels/
24
+ *.egg-info/
25
+ .installed.cfg
26
+ *.egg
27
+ MANIFEST
28
+
29
+ # PyInstaller
30
+ # Usually these files are written by a python script from a template
31
+ # before PyInstaller builds the exe, so as to inject date/other infos into it.
32
+ *.manifest
33
+ *.spec
34
+
35
+ # Installer logs
36
+ pip-log.txt
37
+ pip-delete-this-directory.txt
38
+
39
+ # Unit test / coverage reports
40
+ htmlcov/
41
+ .tox/
42
+ .nox/
43
+ .coverage
44
+ .coverage.*
45
+ .cache
46
+ nosetests.xml
47
+ coverage.xml
48
+ *.cover
49
+ *.py,cover
50
+ .hypothesis/
51
+ .pytest_cache/
52
+
53
+ # Translations
54
+ *.mo
55
+ *.pot
56
+
57
+ # Django stuff:
58
+ *.log
59
+ local_settings.py
60
+ db.sqlite3
61
+ db.sqlite3-journal
62
+
63
+ # Flask stuff:
64
+ instance/
65
+ .webassets-cache
66
+
67
+ # Scrapy stuff:
68
+ .scrapy
69
+
70
+ # Sphinx documentation
71
+ docs/_build/
72
+
73
+ # PyBuilder
74
+ target/
75
+
76
+ # Jupyter Notebook
77
+ .ipynb_checkpoints
78
+
79
+ # IPython
80
+ profile_default/
81
+ ipython_config.py
82
+
83
+ # pyenv
84
+ .python-version
85
+
86
+ # pipenv
87
+ # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
88
+ # However, in case of collaboration, if having platform-specific dependencies or dependencies
89
+ # having no cross-platform support, pipenv may install dependencies that don't work, or not
90
+ # install all needed dependencies.
91
+ #Pipfile.lock
92
+
93
+ # PEP 582; used by e.g. github.com/David-OConnor/pyflow
94
+ __pypackages__/
95
+
96
+ # Celery stuff
97
+ celerybeat-schedule
98
+ celerybeat.pid
99
+
100
+ # SageMath parsed files
101
+ *.sage.py
102
+
103
+ # Environments
104
+ .env
105
+ .venv
106
+ .venv*
107
+ env/
108
+ venv/
109
+ ENV/
110
+ env.bak/
111
+ venv.bak/
112
+ .venv*
113
+
114
+ # Spyder project settings
115
+ .spyderproject
116
+ .spyproject
117
+
118
+ # Rope project settings
119
+ .ropeproject
120
+
121
+ # mkdocs documentation
122
+ /site
123
+
124
+ # mypy
125
+ .mypy_cache/
126
+ .dmypy.json
127
+ dmypy.json
128
+
129
+ # Pyre type checker
130
+ .pyre/
131
+ .vscode/
132
+ .idea/
133
+
134
+ .DS_Store
135
+
136
+ # Pynecone
137
+ pynecone.db
138
+ .web
139
+ node_modules
Dockerfile ADDED
@@ -0,0 +1,66 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # See https://hub.docker.com/r/nikolaik/python-nodejs
2
+ # https://github.com/nikolaik/docker-python-nodejs
3
+ # Default user is 'pn' with uid 1000, gid 1000
4
+ FROM nikolaik/python-nodejs:python3.10-nodejs18
5
+
6
+ # Install nginx and give permissions to 'pn'
7
+ # See https://www.rockyourcode.com/run-docker-nginx-as-non-root-user/
8
+ USER root
9
+
10
+ RUN apt-get -y update && apt-get -y install nginx
11
+
12
+ RUN mkdir -p /var/cache/nginx \
13
+ /var/log/nginx \
14
+ /var/lib/nginx
15
+ RUN touch /var/run/nginx.pid
16
+
17
+ RUN chown -R pn:pn /var/cache/nginx \
18
+ /var/log/nginx \
19
+ /var/lib/nginx \
20
+ /var/run/nginx.pid
21
+
22
+ # Install dependencies and build app as non-root
23
+ USER pn
24
+ ENV HOME=/home/pn \
25
+ PATH=/home/pn/.local/bin:$PATH
26
+
27
+ RUN mkdir $HOME/app
28
+
29
+ WORKDIR $HOME/app
30
+
31
+ # Install bun manually
32
+ RUN curl -fsSL https://bun.sh/install | bash
33
+
34
+ # Install pynecone (in case it's forgotten in requirements.txt)
35
+ RUN pip install --no-cache-dir --upgrade pynecone
36
+
37
+ # Install additional dependencies
38
+ # To install using a package manager (pipenv, poetry, pdm,...) please adapt this section
39
+ COPY --chown=pn requirements.txt requirements.txt
40
+ RUN pip install --no-cache-dir -r requirements.txt
41
+
42
+ # Init project
43
+ RUN pc init
44
+
45
+ # Init frontend
46
+ # Optional: could be done at runtime. But saves time if already done in container build.
47
+ # It installs all frontend dependencies
48
+ RUN python -c "from pathlib import Path; from pynecone.utils import setup_frontend; setup_frontend(Path.cwd())"
49
+
50
+ # Copy existing project
51
+ COPY --chown=pn pcconfig_docker.py pcconfig.py
52
+ COPY --chown=pn default_app default_app
53
+ COPY --chown=pn assets assets
54
+
55
+ # Re-init frontend (if new deps from project)
56
+ RUN python -c "from pathlib import Path; from pynecone.utils import setup_frontend; setup_frontend(Path.cwd())"
57
+
58
+ # Copy nginx configuration
59
+ COPY --chown=pn nginx.conf /etc/nginx/sites-available/default
60
+
61
+ # Copy README and entrypoint script
62
+ COPY --chown=pn README.md README.md
63
+ COPY --chown=pn run.sh .
64
+
65
+ # Run script ('service nginx start' + 'pc run')
66
+ CMD ["bash", "run.sh"]
README.md ADDED
@@ -0,0 +1,122 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ title: Pynecone On Spaces Template
3
+ emoji: 🐿️
4
+ colorFrom: yellow
5
+ colorTo: blue
6
+ sdk: docker
7
+ app_port: 4444
8
+ pinned: false
9
+ license: mit
10
+ duplicated_from: Wauplin/pynecone-on-spaces-template
11
+ ---
12
+
13
+ Deploy your own Pynecone app to Huggingface Spaces!
14
+
15
+ Please be aware that this is still a very recent project, still under active maintenance.
16
+
17
+ If you encounter any issue, please let us know by creating a discussion in our [Community tab](https://huggingface.co/spaces/Wauplin/pynecone-on-spaces-template/discussions).
18
+
19
+ ### Instructions:
20
+
21
+
22
+ #### 1. Install deployment tool
23
+
24
+
25
+ Install the deployment script [from source](https://github.com/Wauplin/pynecone-on-spaces):
26
+
27
+ ```
28
+ pip install git+https://github.com/Wauplin/pynecone-on-spaces
29
+ ```
30
+
31
+ #### 2. (optional) Login to Huggingface
32
+
33
+
34
+ If you don't already have an account, please [register](https://huggingface.co/join) to HF.
35
+
36
+ You will next need to [setup a token](https://huggingface.co/settings/tokens) with `WRITE` permissions.
37
+
38
+ Once you have it, login your machine to your account by running the following command in your terminal:
39
+
40
+ ```bash
41
+ huggingface-cli login
42
+ ```
43
+
44
+ #### 3. Deploy 🚀
45
+
46
+
47
+ Simply deploy your pynecone app:
48
+
49
+ ```bash
50
+ huggingface-pynecone deploy
51
+ ```
52
+
53
+ **Output:**
54
+ ```txt
55
+ New Space created: https://huggingface.co/spaces/Wauplin/clock
56
+ README.md file not found locally. Adapting the existing remote one.
57
+ Setup pcconfig.py
58
+ Setup Dockerfile
59
+ Sync requirements.txt
60
+ Sync assets/ folder
61
+ Sync app folder
62
+ Your app has been successfully deployed!
63
+ Check it out: https://Wauplin-clock.hf.space
64
+ ```
65
+
66
+ The name of your repository will be derived from the app name defined in your `pcconfig.py` file.
67
+
68
+ For more flexibility, you can specify some arguments:
69
+
70
+ ```bash
71
+ huggingface-pynecone deploy --pcconfig_path="path/to/pcconfig.py" --private --token="hf_xxx"
72
+ ```
73
+
74
+ #### 4. Sync changes
75
+
76
+
77
+ Your project is now hosted in a versioned repository on Huggingface.
78
+
79
+ You can decide to clone the repository and push your changes as you would do with any project.
80
+ Please make sure to not break anything in `README.md` (metadata), `Dockerfile` and `pcconfig_docker.py`.
81
+
82
+ Otherwise, you can make changes to your existing project and push your changes using the same command:
83
+
84
+ ```bash
85
+ huggingface-pynecone deploy
86
+ ```
87
+
88
+ **Output:**
89
+
90
+ ```txt
91
+ Repo ID: Wauplin/clock
92
+ Space already exist: https://huggingface.co/spaces/Wauplin/clock
93
+ Space already configured. Do not update README.md, Dockerfile and pcconfig.py
94
+ Sync requirements.txt
95
+ Sync assets/ folder
96
+ Sync app folder
97
+ Your app has been successfully deployed!
98
+ Check it out: https://Wauplin-clock.hf.space
99
+ ```
100
+
101
+
102
+ ### What tweaks are used to deploy you apps?
103
+
104
+
105
+ Some tweaks are necessary to make your Pynecone deployed on HF Spaces.
106
+ Everything is taken care of by the `huggingface-pynecone` CLI tool.
107
+
108
+ List of small tweaks:
109
+ - Running in a Docker Space means only 1 open port:
110
+ - We use nginx proxy to expose both frontend and backend on same port 4444
111
+ - Port 3000 (frontend) is redirected to localhost:4444/
112
+ - Port 8000 (backend) is redirected to localhost:4444/pynecone-backend
113
+ - Port 4444 is set in several files:
114
+ - `README.md`: set `app_port` parameter
115
+ - `nginx.conf`: define which port to listen
116
+ - `pcconfig.py`: monkey-patch pynecone to make it think the backend is running on port 8000 even though a different `api_url` is set
117
+ - `pc.Config` configuration:
118
+ - set env as `PROD`
119
+ - set `api_url` to the Space url + pynecone-backend path. Example: `"https://wauplin-pynecone-counter.hf.space/pynecone-backend"`
120
+ - set `port` to 3000 (frontend port)
121
+ - set db_url to `"sqlite:///pynecone.db"` but DB is not persisted across when the Space is reloaded (i.e.: not really a database yet)
122
+ - Use a `run.sh` script to start nginx before running pynecone server
assets/favicon.ico ADDED
default_app/__init__.py ADDED
File without changes
default_app/default_app.py ADDED
@@ -0,0 +1,58 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from pathlib import Path
2
+
3
+ import pynecone as pc
4
+
5
+
6
+ class State(pc.State):
7
+ count: int = 0
8
+
9
+ def increment(self):
10
+ self.count += 1
11
+
12
+ def decrement(self):
13
+ self.count -= 1
14
+
15
+
16
+ def index():
17
+ return pc.center(
18
+ pc.vstack(
19
+ pc.box(
20
+ height="20px",
21
+ ),
22
+ pc.box(
23
+ pc.heading("Pynecone x Spaces 🚀"),
24
+ ),
25
+ pc.hstack(
26
+ pc.button(
27
+ "Decrement",
28
+ color_scheme="red",
29
+ border_radius="1em",
30
+ on_click=State.decrement,
31
+ ),
32
+ pc.heading(State.count, font_size="2em"),
33
+ pc.button(
34
+ "Increment",
35
+ color_scheme="green",
36
+ border_radius="1em",
37
+ on_click=State.increment,
38
+ ),
39
+ ),
40
+ pc.box(
41
+ height="50px",
42
+ ),
43
+ pc.box(
44
+ # Hacky way to append README content to the page
45
+ pc.markdown(
46
+ (Path(__file__).parent.parent / "README.md")
47
+ .read_text()
48
+ .split("---")[-1]
49
+ .strip()
50
+ )
51
+ ),
52
+ )
53
+ )
54
+
55
+
56
+ app = pc.App(state=State)
57
+ app.add_page(index)
58
+ app.compile()
nginx.conf ADDED
@@ -0,0 +1,29 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ server {
2
+ listen 4444 default_server;
3
+ listen [::]:4444 default_server;
4
+
5
+ server_name _;
6
+
7
+ location / {
8
+ # Serve static frontend from port 3000
9
+ proxy_pass http://localhost:3000;
10
+ proxy_http_version 1.1;
11
+ proxy_set_header Upgrade $http_upgrade;
12
+ proxy_set_header Connection 'upgrade';
13
+ proxy_set_header Host $host;
14
+ proxy_cache_bypass $http_upgrade;
15
+ }
16
+
17
+ location /pynecone-backend/event {
18
+ # Serve backend from port 8000
19
+ proxy_pass http://localhost:8000/event;
20
+ proxy_http_version 1.1;
21
+ proxy_set_header Upgrade $http_upgrade;
22
+ proxy_set_header Connection 'upgrade';
23
+ proxy_set_header Host "localhost:8000";
24
+ proxy_pass_request_headers on;
25
+
26
+ # Disable cache on websockets
27
+ expires -1;
28
+ }
29
+ }
pcconfig.py ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ import pynecone as pc
2
+
3
+ config = pc.Config(
4
+ app_name="default_app",
5
+ db_url="sqlite:///pynecone.db",
6
+ env=pc.Env.DEV,
7
+ )
pcconfig_docker.py ADDED
@@ -0,0 +1,23 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import pynecone as pc
2
+
3
+ config = pc.Config(
4
+ app_name="default_app",
5
+ db_url="sqlite:///pynecone.db",
6
+ api_url="https://wauplin-pynecone-on-spaces-template.hf.space/pynecone-backend",
7
+ env=pc.Env.PROD,
8
+ )
9
+
10
+ ### PATCHING 'get_api_port'
11
+ from pynecone import utils
12
+
13
+ initial_get_api_port = utils.get_api_port
14
+
15
+
16
+ def patched_get_api_port() -> int:
17
+ print(f"Using patched get_api_port (return 8000)")
18
+ return 8000
19
+
20
+
21
+ utils.get_api_port = patched_get_api_port
22
+ print(f"Patched 'get_api_port' to always return 8000.")
23
+ ###
requirements.txt ADDED
@@ -0,0 +1 @@
 
 
1
+ pynecone
run.sh ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ service nginx start
2
+
3
+ pc run --loglevel=info