Skip to content

Commit

Permalink
upd mongo
Browse files Browse the repository at this point in the history
  • Loading branch information
kosarevskiydp committed Dec 17, 2021
1 parent 70db1e4 commit d7d0bfb
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 28 deletions.
42 changes: 15 additions & 27 deletions app.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import streamlit as st
import psycopg2

from mongo import mongo
from postgres import postgres

# st.set_page_config(initial_sidebar_state="collapsed")

Expand All @@ -11,42 +11,30 @@ def header():
for [DB](https://github.com/dKosarevsky/iu7/blob/master/7sem/db.md) course project
in [BMSTU](https://bmstu.ru)
"""
st.markdown("# МГТУ им. Баумана. Кафедра ИУ7")
st.markdown("## Базы данных")
st.header("МГТУ им. Баумана. Кафедра ИУ7")
st.markdown("**Курс:** Базы данных")
st.markdown("**Преподаватель:** Павлюк А.А.")
st.markdown("**Студент:** Косаревский Д.П.")
st.sidebar.image('logo.png', width=300)
st.sidebar.markdown(author)


# Initialize connection.
# Uses st.cache to only run once.
# @st.cache(hash_funcs={builtins.weakref: my_hash_func})
# @st.cache(allow_output_mutation=True, hash_funcs={"_thread.RLock": lambda _: None})
@st.cache(allow_output_mutation=True)
def init_connection():
return psycopg2.connect(**st.secrets["postgres"])


# Perform query.
# Uses st.cache to only rerun when the query changes or after 10 min.
@st.cache(ttl=600)
def run_query(conn, query):
with conn.cursor() as cur:
cur.execute(query)
return cur.fetchall()


def main():
header()

conn = init_connection()
db = st.sidebar.radio(
"Выберите тип БД:", (
"1. Postgres.",
"2. Mongo.",
),
index=0
)

rows = run_query(conn, "SELECT * from information_schema.sql_sizing;")
if db[:1] == "1":
postgres.main()

# Print results.
for row in rows:
st.write(f"{row[0]} has a :{row[1]}:")
elif db[:1] == "2":
mongo.main()


if __name__ == "__main__":
Expand Down
Empty file added mongo/__init__.py
Empty file.
40 changes: 40 additions & 0 deletions mongo/mongo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import streamlit as st
import pymongo


# Pull data from the collection.
# Uses st.cache to only rerun when the query changes or after 10 min.
# @st.cache(ttl=600)
def get_data(client):
db = client.sample_analytics
items = db.customers.find()
items = list(items) # make hashable for st.cache
return items


def main():
st.subheader("Mongo")

# Initialize connection.
client = pymongo.MongoClient(**st.secrets["mongo"])

# test_db = client.db.test
# st.write(test_db)
# test_collection = test_db.test_collection
# st.write(test_collection)
# st.write(test_collection.find_one())

db = client.sample_analytics
st.write(db)
st.write(db.customers.find_one())

# items = get_data(client)

# Print results.
# for item in items:
# st.write(item)
# st.write(f"{item['name']} has a :{item['pet']}:")


if __name__ == "__main__":
main()
Empty file added postgres/__init__.py
Empty file.
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# db
psycopg2-binary==2.9.2 # https://github.com/psycopg/psycopg
python-dotenv==0.12.0
pymongo==4.0.1 # https://pymongo.readthedocs.io/en/stable/
dnspython==2.1.0 # https://pypi.org/project/dnspython/

# Streamlit
streamlit==1.1.0 # https://streamlit.io/
Expand Down

0 comments on commit d7d0bfb

Please sign in to comment.