Zeithrold commited on
Commit
a1eb1f3
·
1 Parent(s): 0bca301

Added code

Browse files
Files changed (3) hide show
  1. .gitignore +176 -0
  2. app.py +67 -0
  3. requirements.txt +4 -0
.gitignore ADDED
@@ -0,0 +1,176 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Created by https://www.toptal.com/developers/gitignore/api/python
2
+ # Edit at https://www.toptal.com/developers/gitignore?templates=python
3
+
4
+ ### Python ###
5
+ # Byte-compiled / optimized / DLL files
6
+ __pycache__/
7
+ *.py[cod]
8
+ *$py.class
9
+
10
+ # C extensions
11
+ *.so
12
+
13
+ # Distribution / packaging
14
+ .Python
15
+ build/
16
+ develop-eggs/
17
+ dist/
18
+ downloads/
19
+ eggs/
20
+ .eggs/
21
+ lib/
22
+ lib64/
23
+ parts/
24
+ sdist/
25
+ var/
26
+ wheels/
27
+ share/python-wheels/
28
+ *.egg-info/
29
+ .installed.cfg
30
+ *.egg
31
+ MANIFEST
32
+
33
+ # PyInstaller
34
+ # Usually these files are written by a python script from a template
35
+ # before PyInstaller builds the exe, so as to inject date/other infos into it.
36
+ *.manifest
37
+ *.spec
38
+
39
+ # Installer logs
40
+ pip-log.txt
41
+ pip-delete-this-directory.txt
42
+
43
+ # Unit test / coverage reports
44
+ htmlcov/
45
+ .tox/
46
+ .nox/
47
+ .coverage
48
+ .coverage.*
49
+ .cache
50
+ nosetests.xml
51
+ coverage.xml
52
+ *.cover
53
+ *.py,cover
54
+ .hypothesis/
55
+ .pytest_cache/
56
+ cover/
57
+
58
+ # Translations
59
+ *.mo
60
+ *.pot
61
+
62
+ # Django stuff:
63
+ *.log
64
+ local_settings.py
65
+ db.sqlite3
66
+ db.sqlite3-journal
67
+
68
+ # Flask stuff:
69
+ instance/
70
+ .webassets-cache
71
+
72
+ # Scrapy stuff:
73
+ .scrapy
74
+
75
+ # Sphinx documentation
76
+ docs/_build/
77
+
78
+ # PyBuilder
79
+ .pybuilder/
80
+ target/
81
+
82
+ # Jupyter Notebook
83
+ .ipynb_checkpoints
84
+
85
+ # IPython
86
+ profile_default/
87
+ ipython_config.py
88
+
89
+ # pyenv
90
+ # For a library or package, you might want to ignore these files since the code is
91
+ # intended to run in multiple environments; otherwise, check them in:
92
+ # .python-version
93
+
94
+ # pipenv
95
+ # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
96
+ # However, in case of collaboration, if having platform-specific dependencies or dependencies
97
+ # having no cross-platform support, pipenv may install dependencies that don't work, or not
98
+ # install all needed dependencies.
99
+ #Pipfile.lock
100
+
101
+ # poetry
102
+ # Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
103
+ # This is especially recommended for binary packages to ensure reproducibility, and is more
104
+ # commonly ignored for libraries.
105
+ # https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
106
+ #poetry.lock
107
+
108
+ # pdm
109
+ # Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
110
+ #pdm.lock
111
+ # pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
112
+ # in version control.
113
+ # https://pdm.fming.dev/#use-with-ide
114
+ .pdm.toml
115
+
116
+ # PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
117
+ __pypackages__/
118
+
119
+ # Celery stuff
120
+ celerybeat-schedule
121
+ celerybeat.pid
122
+
123
+ # SageMath parsed files
124
+ *.sage.py
125
+
126
+ # Environments
127
+ .env
128
+ .venv
129
+ env/
130
+ venv/
131
+ ENV/
132
+ env.bak/
133
+ venv.bak/
134
+
135
+ # Spyder project settings
136
+ .spyderproject
137
+ .spyproject
138
+
139
+ # Rope project settings
140
+ .ropeproject
141
+
142
+ # mkdocs documentation
143
+ /site
144
+
145
+ # mypy
146
+ .mypy_cache/
147
+ .dmypy.json
148
+ dmypy.json
149
+
150
+ # Pyre type checker
151
+ .pyre/
152
+
153
+ # pytype static type analyzer
154
+ .pytype/
155
+
156
+ # Cython debug symbols
157
+ cython_debug/
158
+
159
+ # PyCharm
160
+ # JetBrains specific template is maintained in a separate JetBrains.gitignore that can
161
+ # be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
162
+ # and can be added to the global gitignore or merged into this file. For a more nuclear
163
+ # option (not recommended) you can uncomment the following to ignore the entire idea folder.
164
+ #.idea/
165
+
166
+ ### Python Patch ###
167
+ # Poetry local configuration file - https://python-poetry.org/docs/configuration/#local-configuration
168
+ poetry.toml
169
+
170
+ # ruff
171
+ .ruff_cache/
172
+
173
+ # LSP config files
174
+ pyrightconfig.json
175
+
176
+ # End of https://www.toptal.com/developers/gitignore/api/python
app.py ADDED
@@ -0,0 +1,67 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import pandas as pd
3
+ from tempfile import NamedTemporaryFile
4
+
5
+
6
+ def main(file: str):
7
+ df = pd.read_excel(file)
8
+ df.columns = df.columns.str.strip()
9
+ # Search the max '涨幅' with corresponding '名称' and '代码' for each '细分行业'
10
+ df_max = (
11
+ df.groupby("细分行业")
12
+ .apply(lambda x: x.loc[x["涨幅"].idxmax()], include_groups=False)
13
+ .reset_index()
14
+ )
15
+ df_max = df_max[["细分行业", "名称", "总市值"]]
16
+ # rename 名称 to 领涨股票, 总市值 to 领涨股票市值
17
+ df_max.columns = ["细分行业", "领涨股票", "领涨股票市值"]
18
+ df_max["领涨股票市值"] = df_max["领涨股票市值"] / 1e8
19
+
20
+ # Search the max '涨幅' with corresponding '名称' and '代码' for each '细分行业'
21
+ df_min = (
22
+ df.groupby("细分行业")
23
+ .apply(lambda x: x.loc[x["涨幅"].idxmin()], include_groups=False)
24
+ .reset_index()
25
+ )
26
+ df_min = df_min[["细分行业", "名称", "总市值"]]
27
+ # rename 名称 to 领涨股票, 总市值 to 领涨股票市值
28
+ df_min.columns = ["细分行业", "领跌股票", "领跌股票市值"]
29
+ df_min["领跌股票市值"] = df_min["领跌股票市值"] / 1e8
30
+
31
+ df_count = df.groupby("细分行业").count().reset_index()[["细分行业", "代码"]]
32
+ df_count.columns = ["细分行业", "股票数量"]
33
+ df_total_value = (
34
+ df[["细分行业", "总市值", "流通市值"]].groupby("细分行业").sum().reset_index()
35
+ )
36
+ df_total_value[["总市值", "流通市值"]] = (
37
+ df_total_value[["总市值", "流通市值"]] / 1e8
38
+ )
39
+
40
+ df_result = pd.concat(
41
+ [
42
+ df_count,
43
+ df_total_value[["总市值", "流通市值"]],
44
+ df_max[["领涨股票", "领涨股票市值"]],
45
+ df_min[["领跌股票", "领跌股票市值"]],
46
+ ],
47
+ axis=1,
48
+ )
49
+
50
+ temp_file = NamedTemporaryFile(delete=False, suffix=".xlsx")
51
+ df_result.to_excel(temp_file, index=False)
52
+ return temp_file.name
53
+
54
+
55
+ with gr.Blocks() as demo:
56
+ file_input = gr.File(
57
+ file_count="single",
58
+ file_types=[".xlsx", ".xls"],
59
+ type="filepath",
60
+ label="输入文件",
61
+ )
62
+ file_output = gr.File()
63
+ button = gr.Button("开始处理")
64
+ button.click(main, inputs=[file_input], outputs=[file_output])
65
+
66
+
67
+ demo.launch()
requirements.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ numpy
2
+ gradio
3
+ pandas
4
+ openpyxl